def plot_model(model): from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG( model_to_dot(model, show_shapes=True, show_layer_names=True).create(prog='dot', format='svg'))
def show_full_network(cur_network, out_path='temp_network.svg'): """ Show a graph representation of the network """ dmodel = vu.model_to_dot(cur_network, show_shapes=True) dmodel.write_svg(out_path) return SVG(out_path)
def main(): with open('data/cafa3/model_cc.json') as f: json_string = next(f) model = model_from_json(json_string) graph = model_to_dot(model).create(prog='dot', format='svg') with open('graph.svg', 'w') as f: f.write(SVG(graph).data)
def plot_model(self, show_shapes=True, show_layer_names=True): from keras.utils.visualize_util import plot, model_to_dot from IPython.display import SVG filename = os.path.join(self.dirhelper.get_model_run_path, 'model.png') plot(self.model, to_file=filename, show_shapes=show_shapes, show_layer_names=show_layer_names) dot_data = model_to_dot(self.model, show_shapes=show_shapes, show_layer_names=show_layer_names) svg_data = SVG(dot_data.create(prog='dot', format='svg')) dot_data.write()
def draw_model(model): from IPython.display import SVG from keras.utils.visualize_util import model_to_dot from keras.utils.visualize_util import plot #graph = to_graph(model, show_shape=True) #graph.write_png("UFCNN_1.png") SVG(model_to_dot(model).create(prog='dot', format='svg')) plot(model, to_file='UFCNN_1.png')
def write_model_graph(self): model = self.create_model() # write dot text to file with open(config["model_dot"], "wb") as out: m = model_to_dot(model) dot_text = m.create_dot() out.write(dot_text) print("Wrote .dot to {}".format(config["model_dot"])) # write graph to file plot(model, to_file=config["model_graph"], show_shapes=True)
def renderModel2ImageFromJson(paramFlowJson): tmpParser = DLSDesignerFlowsParser(paramFlowJson) tmpModel, _ = tmpParser.buildKerasTrainer() tmpDot = kervis.model_to_dot(tmpModel, show_shapes=True) tmpStr = tmpDot.create_png(prog='dot') sio = StringIO() sio.write(tmpStr) sio.seek(0) timg = skio.imread(sio) return timg
def build(width, height, depth, classes, weights_path=None): """ :param width: the width of the input images :param height: the height of the input images :param depth: the depth of the input images :param classes: the numbers of labels :param weights_path: URL of an already trained model. :return: a train model. """ # initialize the model.. model = Sequential() # first set of CONV => RELU => POOL model.add(Convolution2D(32, 5, 5, border_mode="same", bias=True, input_shape=(depth, height, width))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # second set of CONV => RELU => POOL => Dropout model.add(Convolution2D(64, 5, 5, border_mode="same", bias=True)) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Dropout(0.25)) # set of FC => RELU layers model.add(Flatten()) # convert convolutional filters to flatt so they can be feed to fully connected layers # first fully connected layer model.add(Dense(400, init='lecun_uniform', bias=True)) # init='glorot_uniform' model.add(Activation("relu")) model.add(Dropout(0.25)) # second fully connected layer.. softmax classifier model.add(Dense(classes, init='lecun_uniform', bias=True)) model.add(Activation("softmax")) # if a weights path is supplied (indicating that the model was pre-trained), then load the weights. if weights_path is not None: model.load_weights(weights_path) # Visualize print "[INFO] Visualize.." SVG(model_to_dot(model).create(prog='dot', format='svg')) plot(model, to_file='/home/rainer85ah/PycharmProjects/PythonProjects/Output/model_9936.jpg', show_shapes=False, show_layer_names=True) # return the constructed network architecture return model
def __init__(self, database, config, model, modelj, resume=None): super(Callback, self).__init__() self.mgdb = database self.config = config if resume is None: self.id = int(time.time()) + randint(0, 50) svgmodel = model_to_dot(model, show_shapes=True).create(prog='dot', format='svg') self.backup = {'_id': self.id, 'host': socket.gethostname().split('.')[0], 'model': modelj, 'svgmodel': svgmodel, 'config': self.config, 'acc': [], 'loss': [], 'val_acc': [], 'val_loss': [], 'time_init': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), 'time_upd': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), 'done': False } try: # Try to save log in DB client = MongoClient(self.mgdb.server) db = client[self.mgdb.db] db.authenticate(self.mgdb.user, password=self.mgdb.passwd) col = db[self.mgdb.col] col.insert(self.backup) except ConnectionFailure: pass else: self.id = config['_id'] self.backup = resume self.backup['host'] = socket.gethostname().split('.')[0] self.backup['time_init'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) self.backup['time_upd'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) try: # Try to save log in DB client = MongoClient(self.mgdb.server) db = client[self.mgdb.db] db.authenticate(self.mgdb.user, password=self.mgdb.passwd) col = db[self.mgdb.col] col.update({'_id': self.id}, {'$set': {'host': self.backup['host']}}) col.update({'_id': self.id}, {'$set': {'time_init': self.backup['time_init']}}) col.update({'_id': self.id}, {'$set': {'time_upd': self.backup['time_upd']}}) except ConnectionFailure: pass
def save(): ######## MODEL VISUALIZATION ################ # XML rendering (lame): https://github.com/mdaines/viz.js/ # D3 rendering: https://github.com/mstefaniuk/graph-viz-d3-js print "creating model vis png" from keras.utils.visualize_util import plot plot(model, to_file=model_vis_dir + 'model.png', show_shapes=True, show_layer_names=True) print "creating model vis raw graphviz to txt" from keras.utils.visualize_util import model_to_dot graphviz_dot = model_to_dot(model) raw_dot_language = graphviz_dot.to_string() with open(model_vis_dir + 'model_dot.txt','wb') as f: f.write(raw_dot_language)
def viz_model(model, file): dot = model_to_dot(model, show_shapes=True, show_layer_names=False) dot.write_pdf(file)
model.add(Activation("relu")) model.add(Dense(output_dim=10)) model.add(Activation("softmax")) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.summary() model.get_config() from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG(model_to_dot(model).create(prog='dot', format='svg')) from keras.models import Sequential from keras.layers import Dense, Activation model = Sequential([ Dense(32, input_dim=784), Activation('relu'), Dense(10), Activation('softmax'), ]) from keras.layers import Merge left_branch = Sequential() left_branch.add(Dense(32, input_dim=784))
final_model = Sequential() final_model.add( Merge([merged_q_cd, merged_q_incd1, merged_q_incd2, merged_q_incd3], mode='concat', name='final_layer')) final_model.add(Dense(final_dense_units, init='uniform', activation='softmax')) print("Compiling the model.... ") final_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print("Neural Network Model Compiled Successfully!") print(final_model.summary()) from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG( model_to_dot(final_model, show_shapes=True).create(prog='dot', format='svg')) print('Train...') final_model.fit( [x_query, x_similar, x_nonsimilar1, x_nonsimilar2, x_nonsimilar3], data_output_labels, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=0.2) print('Model Fitting Completed!')
plt.figure(figsize=(10,10)) ax = plt.subplot(1, 3, 1) plt.imshow(x_train[pred0].reshape((28,28))) plt.gray() plt.title('Original') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax = plt.subplot(1, 3, 2) plt.imshow(a.reshape((shape,shape)),cmap=cm2) plt.title('Prediction') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax = plt.subplot(1, 3, 3) plt.imshow(x_train[pred0].reshape((28,28))) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax = plt.subplot(1, 3, 3) plt.imshow(a.reshape((shape,shape)),cmap=cm,alpha=0.35,interpolation='nearest') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.title('MASK') plt.show() import pydot import graphviz import pydot_ng as pydot from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG(model_to_dot(recog_right).create(prog='dot', format='svg'))
GAN.fit(x_train[0].reshape(1, 784), x_train[0].reshape((1, 784)), batch_size=30, nb_epoch=1, verbose=1) x_train_GAN = x_train[0].reshape(1, 784) a = GAN.predict(x_train[0].reshape(1, 784), verbose=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(1, 2, 1) plt.imshow(x_train_GAN.reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax = plt.subplot(1, 2, 2) plt.imshow(a.reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() import pydot import graphviz import pydot_ng as pydot from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG(model_to_dot(recog_right).create(prog='dot', format='svg'))
tracks = layers.Input(shape=(None, 4), name='tracks') rnnip_raw = layers.GRU(5, name='rnnip_lstm')(tracks) vx_inputs = layers.Input(shape=(1,), name='vertex_info') ip3d_inputs = layers.Input(shape=(3,), name='ip3d') dl1_inputs = layers.merge([vx_inputs,ip3d_inputs], mode='concat') #4 dl_inputs = layers.merge([rnnip_raw, vx_inputs], mode='concat') #6 dl1_first_layer = layers.Dense(6, name='DL1_layer1')(dl1_inputs) dl1_out_layer = layers.Dense(4, name='DL1_shared', activation='softmax') dl1 = dl1_out_layer(dl1_first_layer) dl2 = dl1_out_layer(dl_inputs) rnnip = layers.Dense(4, name='rnnip_out')(rnnip_raw) model = Model(input=[tracks, vx_inputs, ip3d_inputs], output=[dl1, dl2, rnnip]) model.compile(optimizer='adam', loss='categorical_crossentropy') with open('ftag-arch.json','w') as archetecture: archetecture.write(model.to_json(indent=2, sort_keys=True) ) model.save_weights('ftag-weights.h5') from keras.utils.visualize_util import model_to_dot model_to_dot(model).write_pdf('ftag-model.pdf') trk = np.linspace(-1, 1, 20)[:,None] * np.linspace(-1, 1, 4)[None,:] vx = np.array([0])[None,:] ip3d = np.linspace(-1, 1, 3)[None,:] for output in model.predict([trk[None,:], vx, ip3d]): print(output)
x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(encoded) x = UpSampling2D((2, 2))(x) x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x) x = UpSampling2D((2, 2))(x) decoded = Convolution2D(3, 3, 3, activation='sigmoid', border_mode='same')(x) autoencoder = Model(input_img, decoded) autoencoder.compile(optimizer='adadelta', loss='mean_absolute_error') from keras.utils.visualize_util import plot plot(autoencoder, to_file='model.png', show_shapes=True, show_layer_names=True) from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG(model_to_dot(autoencoder).create(prog='dot', format='svg')) hist = autoencoder.fit(x_train, x_train, nb_epoch=10, batch_size=128, shuffle=True, validation_data=(x_test_cropped, x_test), callbacks=[ TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False) ]) decoded_imgs = autoencoder.predict(x_test_cropped) print decoded_imgs.shape
model.add(Dropout(0.2)) model.add(Dense(hidden_dims1, b_regularizer=l2(0.01)), ) model.add(Dense(hidden_dims2, b_regularizer=l2(0.01)), ) model.add(Dense(hidden_dims3, activation='softsign')) model.compile(loss='mean_absolute_error', optimizer='adam', metrics=[matthews_correlation]) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=train_params['LSTM'][emoNames[EMOTION]]['nb_epoch'],validation_split=None,) # In[43]: from IPython.display import SVG # from keras.utils.vis_utils import model_to_dot from keras.utils.visualize_util import model_to_dot s=SVG(model_to_dot(model,show_shapes=True,show_layer_names=False).create(prog='dot', format='svg')) s # In[242]: # Load ready model from keras.models import load_model, model_from_json def _load_model_emo_and_weights(filename, emo): with open(filename+'.'+emo+'.json', 'r') as json_file: loaded_model_json = json_file.read() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights(filename+'.'+emo+'.h5') return loaded_model
def draw_model_graph(model): return SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
model22.add(Activation('relu')) model22.add(Reshape((8,))) model22.add(Dense(5)) model22.add(BatchNormalization(mode=2)) model22.add(Activation('relu')) model22.add(Dense(3)) model22.add(Activation('sigmoid')) model22.compile(loss = 'binary_crossentropy', optimizer = sgd, metrics = ['accuracy']) model22.summary() model22.fit([X_train2,X_train2,X_train2], Y_train, batch_size = 30, nb_epoch = 300, verbose = 1,validation_split=0.9) res22 = model22.predict_classes([X_train2,X_train2,X_train2],batch_size = 30) acc22=((res22-data2.ix[:,4])==0).sum()/len(res22) acc22 import pydot import graphviz import pydot_ng as pydot from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG(model_to_dot(model22).create(prog='dot', format='svg'))
### 入力層は Embeddding で固定次元に変換 cbow.add( Embedding(input_dim=V, output_dim=dim, init='glorot_uniform', input_length=window_size * 2)) ### 入力層の出力を、dim 次元ベクトルの平均値にする cbow.add(Lambda(lambda x: K.mean(x, axis=1), output_shape=(dim, ))) ### dim 次元に変換された 4 つの単語を入力とする第二層 cbow.add(Dense(V, init='glorot_uniform', activation='softmax')) ### スケーラブル・ベクター・グラフィックを生成、表示 SVG(model_to_dot(cbow, show_shapes=True).create(prog='dot', format='svg')) # In[9]: ### 学習過程の確認 cbow.compile(loss='categorical_crossentropy', optimizer="adadelta") # In[10]: ### 学習開始 for ite in range(10): loss = 0. for x, y in generate_data(corpus, window_size, V): loss += cbow.train_on_batch(x, y) print(ite, loss)
combined_model = models.Model(input=synthetic_image_tensor, output=[refiner_model_output, combined_output], name='combined') discriminator_model_output_shape = discriminator_model.output_shape print(refiner_model.summary()) print(discriminator_model.summary()) print(combined_model.summary()) # Visualization from keras.utils.visualize_util import model_to_dot from IPython.display import SVG # Define model try: model_to_dot(refiner_model, show_shapes=True).write_svg('refiner_model.svg') SVG('refiner_model.svg') except ImportError: print('Not running the patched version of keras/pydot!') pass try: model_to_dot(discriminator_model, show_shapes=True).write_svg('discriminator_model.svg') SVG('discriminator_model.svg') except ImportError: pass # Losses #
# In[8]: ### ネットワーク構造をシーケンシャルに cbow = Sequential() ### 入力層は Embeddding で固定次元に変換 cbow.add(Embedding(input_dim=V, output_dim=dim, init='glorot_uniform',input_length=window_size*2)) ### 入力層の出力を、dim 次元ベクトルの平均値にする cbow.add(Lambda(lambda x: K.mean(x, axis=1), output_shape=(dim,))) ### dim 次元に変換された 4 つの単語を入力とする第二層 cbow.add(Dense(V, init='glorot_uniform', activation='softmax')) ### スケーラブル・ベクター・グラフィックを生成、表示 SVG(model_to_dot(cbow, show_shapes=True).create(prog='dot', format='svg')) # In[9]: ### 学習過程の確認 cbow.compile(loss='categorical_crossentropy', optimizer="adadelta") # In[10]: ### 学習開始 for ite in range(10): loss = 0. for x, y in generate_data(corpus, window_size, V): loss += cbow.train_on_batch(x, y)
x = MaxPooling2D((2, 2), border_mode='same')(x) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(x) x = MaxPooling2D((2, 2), border_mode='same')(x) x = Flatten()(x) encoded = Dense(2000)(x) oneD = Dense(BaseLevel*BaseLevel*128)(encoded) fold = Reshape((BaseLevel,BaseLevel,128))(oneD) x = UpSampling2D((2, 2))(fold) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(x) x = UpSampling2D((2, 2))(x) decoded = Convolution2D(3, 3, 3, activation='sigmoid', border_mode='same')(x) autoencoder = Model(input_img, decoded) autoencoder.compile(optimizer='adam', loss='binary_crossentropy') SVG(model_to_dot(autoencoder,show_shapes=True).create(prog='dot', format='svg')) # In[4]: # trainX =np.expand_dims(backtorgb, 0) # trainy =np.expand_dims(colorImg, 0) # In[ ]: tensorBoardPath = '/home/yhfy2006/machineLearningInPython/AutoEncoder/logs' tb_cb = keras.callbacks.TensorBoard(log_dir=tensorBoardPath, histogram_freq=1) checkpoint = ModelCheckpoint(filepath="/home/yhfy2006/machineLearningInPython/AutoEncoder/logs/weights.hdf5", verbose=1, save_best_only=True) cbks = [checkpoint]
from IPython.display import SVG from keras.utils.visualize_util import plot, model_to_dot from TrainHeatingClassifier import loadModelParamClassifier import io pModelWeights = "model_weights_v2" pModelConfig = "model_config_v2" heatingParamAI = loadModelParamClassifier(pModelConfig, pModelWeights) svg_img = (model_to_dot(heatingParamAI, show_shapes=True).create(prog='dot', format='svg')) file('heatingParamAI.svg', 'w').write(svg_img) #plot(heatingParamAI, to_file='heatingParamAI.png', show_shapes=True)
# text + img => GRU x = merge([img_input, lang_embed], mode='concat', concat_axis=-1) x = Reshape((1, lang_dim + img_dim))(x) x = GRU(128)(x) # predict next word out = Dense(vocab_size, activation='softmax')(x) model = Model(input=[img_input, lang_input], output=out) # choose objective and optimizer model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=1e-3)) return model model = image_caption_model() display( SVG( model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))) vocab = cPickle.load(open('dataset/text/vocab.pkl', 'rb')) print('total {} vocabularies'.format(len(vocab))) # total 26900 vocabularies def count_vocab_occurance(vocab, df): voc_cnt = {v: 0 for v in vocab} for img_id, row in df.iterrows(): for w in row['caption'].split(' '): voc_cnt[w] += 1 return voc_cnt