def get_CNN(shape=[100,176,256, 256],optimizer='adam', learning_rate=0.001, loss='categorical_crossentropy'): if platform == 'win32': root = 'C:/Users/douce/Desktop/MIT Fall 2018/6.869 Machine Vision/Final Project/' else: root = '/home/ubuntu' tf.reset_default_graph() net = tflearn.input_data(shape) net = tflearn.conv_3d(net, 16, 5, strides=2, activation='leaky_relu', padding='VALID', weights_init='xavier', regularizer='L2', weight_decay=0.01) net = tflearn.max_pool_3d(net, kernel_size=3, strides=2, padding='VALID') net = tflearn.conv_3d(net, 32, 3, strides=2, padding='VALID', weights_init='xavier', regularizer='L2', weight_decay=0.01) net = tflearn.normalization.batch_normalization(net) net = tflearn.activations.leaky_relu(net) net = tflearn.max_pool_3d(net, kernel_size=2, strides=2, padding='VALID') net = tflearn.dropout(net, 0.5) net = tflearn.fully_connected(net, 1024, weights_init='xavier', regularizer='L2') net = tflearn.normalization.batch_normalization(net, gamma=1.1, beta=0.1) net = tflearn.activations.leaky_relu(net) net = tflearn.dropout(net, 0.6) net = tflearn.fully_connected(net, 512, weights_init='xavier', regularizer='L2') net = tflearn.normalization.batch_normalization(net, gamma=1.2, beta=0.2) net = tflearn.activations.leaky_relu(net) net = tflearn.dropout(net, 0.7) net = tflearn.fully_connected(net, 128, weights_init='xavier', regularizer='L2') net = tflearn.normalization.batch_normalization(net, gamma=1.4, beta=0.4) net = tflearn.activations.leaky_relu(net) net = tflearn.dropout(net, 0.7) net = tflearn.fully_connected(net, 3, weights_init='xavier', regularizer='L2') net = tflearn.normalization.batch_normalization(net, gamma=1.3, beta=0.3) net = tflearn.activations.softmax(net) net = tflearn.regression(net, optimizer='adam', learning_rate=0.001, loss='categorical_crossentropy') model = tflearn.DNN(net, checkpoint_path=os.path.join(root, 'model.tfl.ckpt'), max_checkpoints=3)
def main(): ''' Establishes architecture for CNN-fully-connected-RNN neural net. ''' # Create the input layer: None: batch size, 120: frames, 80: height, 80: width, 3: RGB network = tflearn.input_data(shape=[None, 120, 80, 80, 3], name='input') # Convolutional network network = tflearn.conv_3d( network, 32, (3, 3, 3), activation='relu' ) # 32 conv layers of 3x3x3 (3x3x3 convolves for each 3 frames, 3px height, and 3px width) network = tflearn.max_pool_3d( network, (1, 2, 2), strides=(1, 2, 2)) # Pools results of the conv_3d layer network = tflearn.conv_3d(network, 64, (3, 3, 3), activation='relu') # 64 layers of 3x3x3 network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2)) network = tflearn.conv_3d(network, 128, (3, 3, 3), activation='relu') # 128 layers of 3x3x3 network = tflearn.conv_3d(network, 128, (3, 3, 3), activation='relu') # another one? network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2)) network = tflearn.conv_3d(network, 256, (2, 2, 2), activation='relu') # 256 layers of 2x2x2 network = tflearn.conv_3d(network, 256, (2, 2, 2), activation='relu') network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2)) network = tflearn.conv_2d(network, 64, 4, activation='relu', regularizer="L2") # 64 layers of 4x4 network = tflearn.max_pool_2d(network, 2) # and then max pool # Normalize activations of the previous layer at each batch. network = tflearn.local_response_normalization(network) # And now the fully-connected neural net (128 & 256 neurons + dropout) network = tflearn.fully_connected(network, 128, activation='tanh') network = tflearn.dropout(network, 0.8) network = tflearn.fully_connected(network, 256, activation='tanh') network = tflearn.dropout(network, 0.8) network = tflearn.reshape(network, [-1, 1, 256]) # Why 256? # LSTM layers network = tflearn.lstm(network, 128, return_seq=True) # LSTM of 128 units network = tflearn.lstm(network, 128) network = tflearn.fully_connected( network, 4, activation='softmax') # Just four neurons... okay? network = tflearn.regression(network, optimizer='adam', loss='categorical_crossentropy', name='target') model = tflearn.DNN(network, tensorboard_verbose=0) X, Y = getXY() model.fit(X, Y, n_epoch=1, validation_set=0.1, show_metric=True, snapshot_step=100)
def inference(images, seismic, dropout_keep): with tf.variable_scope("inference"): # Image Model image_feats = image_features(images) # Seismic Model seismic_feats = seismic_features(seismic) # Fusion with tf.variable_scope("Fusion"): i_shape = image_feats.get_shape() s_shape = seismic_feats.get_shape() output_shape = [ FLAGS.batch_size, int(i_shape[1]), int(i_shape[2]), int(s_shape[1]), int(i_shape[3] + s_shape[3]) ] # Bx7x7x39x3072 # ipdb.set_trace() i_arranged = tf.expand_dims(image_feats, 3) F1 = tf.tile(i_arranged, [1, 1, 1, output_shape[3], 1]) s_arranged = tf.transpose(tf.expand_dims( seismic_feats, 1), [0, 1, 3, 2, 4]) # has dims 256,1,1,19,20 F2 = tf.tile(s_arranged, [1, output_shape[1], output_shape[2], 1, 1]) # F1_norm = slim.batch_norm # bil_feats = tf.batch_matmul(F1, F2, name="Tensor_Product") # ipdb.set_trace() spatio_temporal_feats = tf.concat([F1, F2], 4) tf.add_to_collection( 'feats', [seismic_feats, image_feats, F2, F1, spatio_temporal_feats]) # ipdb.set_trace() print('asf') c3_1 = tflearn.conv_3d(spatio_temporal_feats, 128, 3, activation='relu', weights_init='normal') c3_p = tflearn.max_pool_3d(c3_1, kernel_size=[1, 2, 2, 5, 1], strides=[1, 2, 2, 5, 1]) tf.add_to_collection('WtoRegu', c3_1.W) # ipdb.set_trace() # c3_2 = tflearn.conv_3d(c3_p, 64, 3, activation='relu', weights_init='normal') # c3_p2 = tflearn.max_pool_3d(c3_2, kernel_size=[1, 2, 2, 5, 1], strides=[1, 2, 2, 5, 1]) # ipdb.set_trace() final_feats = tf.reshape(c3_p, [FLAGS.batch_size, -1]) # Classifier logits = classification(final_feats, dropout_keep) return logits
datanp=[] #images truenp=[] #labels for file in os.listdir(data_dir): data=np.load(os.path.join(data_dir,file)) datanp.append((data[0][0])) truenp.append(data[0][1]) sh=datanp.shape tf.reset_default_graph() net = tflearn.input_data(shape=[None, sh[1], sh[2], sh[3], sh[4]]) net = tflearn.conv_3d(net, 16,5,strides=2,activation='leaky_relu', padding='VALID',weights_init='xavier',regularizer='L2',weight_decay=0.01) net = tflearn.max_pool_3d(net, kernel_size = 3, strides=2, padding='VALID') net = tflearn.conv_3d(net, 32,3,strides=2, padding='VALID',weights_init='xavier',regularizer='L2',weight_decay=0.01) net = tflearn.normalization.batch_normalization(net) net = tflearn.activations.leaky_relu (net) net = tflearn.max_pool_3d(net, kernel_size = 2, strides=2, padding='VALID') net = tflearn.dropout(net,0.5) net = tflearn.fully_connected(net, 1024,weights_init='xavier',regularizer='L2') net = tflearn.normalization.batch_normalization(net,gamma=1.1,beta=0.1) net = tflearn.activations.leaky_relu (net) net = tflearn.dropout(net,0.6) net = tflearn.fully_connected(net, 512,weights_init='xavier',regularizer='L2') net = tflearn.normalization.batch_normalization(net,gamma=1.2,beta=0.2) net = tflearn.activations.leaky_relu (net) net = tflearn.dropout(net,0.7) net = tflearn.fully_connected(net, 128,weights_init='xavier',regularizer='L2') net = tflearn.normalization.batch_normalization(net,gamma=1.4,beta=0.4)
test = 'test' train = 'training' def getXY(): X, Y = image_preloader(train, image_shape=(80, 80), mode='folder', categorical_labels='True', normalize=True) return X, Y network = tflearn.input_data(shape=[None, 120, 80, 80, 3], name='input') network = tflearn.conv_3d(network, 32, (3, 3, 3), activation='relu') network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2)) network = tflearn.conv_3d(network, 64, (3, 3, 3), activation='relu') network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2)) network = tflearn.conv_3d(network, 128, (3, 3, 3), activation='relu') network = tflearn.conv_3d(network, 128, (3, 3, 3), activation='relu') network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2)) network = tflearn.conv_3d(network, 256, (2, 2, 2), activation='relu') network = tflearn.conv_3d(network, 256, (2, 2, 2), activation='relu') network = tflearn.max_pool_3d(network, (1, 2, 2), strides=(1, 2, 2)) network = tflearn.conv_2d(network, 64, 4, activation='relu', regularizer="L2") network = tflearn.max_pool_2d(network, 2) network = tflearn.local_response_normalization(network) network = fully_connected(network, 128, activation='tanh') network = dropout(network, 0.8) network = fully_connected(network, 256, activation='tanh')