Skip to content

Instantly share code, notes, and snippets.

@philographer
Created May 12, 2019 12:29
Show Gist options
  • Save philographer/d2a2253d34890b2697c7278b3b99a5b1 to your computer and use it in GitHub Desktop.
Save philographer/d2a2253d34890b2697c7278b3b99a5b1 to your computer and use it in GitHub Desktop.
민식이 과제
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import tensorflow as tf
seed=0
np.random.seed(seed)
tf.set_random_seed(seed)
df=pd.read_csv("./dataset.csv")
df.head()
dataset=df.values
x=dataset[:,1:6]
y=dataset[:,0]
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.3, random_state=seed)
model=Sequential()
model.add(Dense(30, input_dim=5, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=500, batch_size=20)
y_prediction=model.predict(x_test).flatten()
for i in range(20):
label=y_test[i]
prediction=y_prediction[i]
print("실제DISP : {:.3f}, 예상DISP : {:.3f}".format(label, prediction))
model.save('it.h5')
model.to_json()
with open('model.json', 'w') as f:
f.write(model.to_json())
from keras.models import load_model
model = load_model('./it.h5')
ship_element=np.array([141.1,24.2,13.2,9.714,0.9866])
ship_element=np.expand_dims(ship_element, axis=0)
model.predict(ship_element)
import os
import os.path as osp
import argparse
import tensorflow as tf
from keras.models import load_model
from keras import backend as K
def convertGraph( modelPath, outdir, numoutputs, prefix, name):
if not os.path.isdir(outdir):
os.mkdir(outdir)
K.set_learning_phase(0)
net_model = load_model(modelPath)
pred = [None]*numoutputs
pred_node_names = [None]*numoutputs
for i in range(numoutputs):
pred_node_names[i] = prefix+'_'+str(i)
pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i])
print('Output nodes names are: ', pred_node_names)
sess = K.get_session()
f = 'graph_def_for_reference.pb.ascii'
tf.train.write_graph(sess.graph.as_graph_def(), outdir, f, as_text=True)
print('Saved the graph definition in ascii format at: ', osp.join(outdir, f))
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
graph_io.write_graph(constant_graph, outdir, name, as_text=False)
print('Saved the constant graph (ready for inference) at: ', osp.join(outdir, name))
convertGraph("it.h5","./",2,"k2tfout","output_graph.pb")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment