def inference(x, y_, keep_prob, phase_train): with tf.variable_scope('conv_1'): conv1 = Convolution2D(x, (28, 28), 1, 32, (5, 5), activation='none') conv1_bn = batch_norm(conv1.output(), 32, phase_train) conv1_out = tf.nn.relu(conv1_bn) pool1 = MaxPooling2D(conv1_out) pool1_out = pool1.output() with tf.variable_scope('conv_2'): conv2 = Convolution2D(pool1_out, (14, 14), 32, 64, (5, 5), activation='none') conv2_bn = batch_norm(conv2.output(), 64, phase_train) conv2_out = tf.nn.relu(conv2_bn) pool2 = MaxPooling2D(conv2_out) pool2_out = pool2.output() pool2_flat = tf.reshape(pool2_out, [-1, 7 * 7 * 64]) with tf.variable_scope('fc1'): fc1 = FullConnected(pool2_flat, 7 * 7 * 64, 1024) fc1_out = fc1.output() fc1_dropped = tf.nn.dropout(fc1_out, keep_prob) y_pred = ReadOutLayer(fc1_dropped, 1024, 10).output() loss = tf.reduce_mean( -tf.reduce_sum(y_ * tf.log(y_pred), reduction_indices=[1])) accuracy = evaluation(y_pred, y_) return loss, accuracy, y_pred
def create_net(self): self.layers = [self.x_image] with tf.variable_scope('encoder') as vs: self.create_encoder_conv([2]) self.create_encoder_fc([5]) self.latent = FullConnected( self.layers[-1], self.layers[-1].get_shape().as_list()[1], self.latent_dim, activation='identity', name="latent").output() self.layers.append(self.latent) with tf.variable_scope('decoder') as vs: self.create_decoder_fc([5, self.traj_length * 2], [-1, self.traj_length, 2, 1]) self.create_decoder_conv([2, 1]) print("Last layer") print(self.layers[-1].get_shape().as_list()) res = tf.reshape(self.layers[-1], [-1, self.traj_length * 2], name="reconstructed") self.layers.append(res) self.decoded = FullConnected( self.layers[-1], self.layers[-1].get_shape().as_list()[1], 100, activation='identity', name="decoded").output() self.layers.append(self.decoded)
def create_decoder_fc(self, conf, final_shape): for i in range(len(conf)): if i > 1: drop = tf.nn.dropout(self.layers[-1], self.keep_prob) self.layers.append(drop) fc = FullConnected(self.layers[-1], self.layers[-1].get_shape().as_list()[1], conf[i], activation='sigmoid') self.layers.append(fc.output()) self.layers.append(tf.reshape(self.layers[-1], final_shape))
def create_encoder_fc(self, conf): size = self.layers[-1].get_shape().as_list() self.layers.append( tf.reshape(self.layers[-1], [-1, size[1] * size[2] * size[3]])) for i in range(len(conf)): if i > 1: drop = tf.nn.dropout(self.layers[-1], self.keep_prob) self.layers.append(drop) fc = FullConnected(self.layers[-1], self.layers[-1].get_shape().as_list()[1], conf[i], activation='sigmoid') self.layers.append(fc.output())
def mk_nn_model(x, y_): # Encoding phase x_image = tf.reshape(x, [-1, 28, 28, 1]) conv1 = Convolution2D(x_image, (28, 28), 1, 8, (9, 9), activation='sigmoid') conv1_out = conv1.output() # pool1 = MaxPooling2D(conv1_out) # pool1_out = pool1.output() # pool1_out = tf.nn.dropout(pool1_out,keep_prob=0.2) conv2 = Convolution2D(conv1_out, (28, 28), 8, 4, (9, 9), activation='sigmoid') conv2_out = conv2.output() # pool2 = MaxPooling2D(conv2_out) # pool2_out = pool2.output() # pool2_out = tf.nn.dropout(pool2_out,keep_prob=0.2) # at this point the representation is (4, 28, 28) i.e. 128*16-dimensional po = tf.reshape(conv2_out,[-1,4*28*28]) fc = FullConnected(po, 4*28*28, 256, activation='sigmoid') fc_out = fc.output() # fc2 = FullConnected(fc_out, 256, 2, activation='sigmoid') # fc2_out = fc2.output() fo = FullConnected(fc_out, 256, 10, activation='sigmoid') fo_out = fo.output() # Decoding phase dfc1 = FullConnected(fo_out, 10, 256, activation='sigmoid') dfc1_out = dfc1.output() # reshape deconvin = tf.reshape(dfc1_out, [-1,16,16,1]) #resize_images(images, size, method=ResizeMethod.BILINEAR, align_corners=False) deconvin = tf.image.resize_images(deconvin, (28,28),method=tf.image.ResizeMethod.BILINEAR, align_corners=False) conv_t1 = Conv2Dtranspose(deconvin, (28, 28), 1, 4, (12, 12), activation='sigmoid') conv_t1_out = conv_t1.output() conv_t2 = Conv2Dtranspose(conv_t1_out, (28, 28), 4, 4, (17, 17), activation='sigmoid') conv_t2_out = conv_t2.output() conv_t3 = Conv2Dtranspose(conv_t2_out, (28, 28), 4, 1, (1, 1), activation='sigmoid') decoded = conv_t3.output() decoded = tf.reshape(decoded, [-1, 784]) cross_entropy = -1. *x *tf.log(decoded) - (1. - x) *tf.log(1. - decoded) loss = tf.reduce_mean(cross_entropy) # crossentry for classifier cross_entropy_acc = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=fo_out) lossacc = tf.reduce_mean(cross_entropy_acc) # accuracy of the trained model, between 0 (worst) and 1 (best) correct_prediction = tf.equal(tf.argmax(fo_out, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) return loss, decoded, lossacc, fo_out, accuracy
def mk_nn_model(x, y_): # Encoding phase x_image = tf.reshape(x, [-1, 227, 227, 3]) #1st conv. conv1 = Convolution2D(x_image, (227, 227), 3, 96, (7, 7), activation='relu', S=4) conv1_out = conv1.output() #1st pooling pool1 = MaxPooling2D(conv1_out, ksize=[1, 3, 3, 1], S=2) pool1_out = pool1.output() #dropout? # pool1_out = tf.nn.dropout(pool1_out,keep_prob=0.2) #LRN1 norm1 = tf.nn.local_response_normalization(pool1_out, depth_radius=5, alpha=0.0001, beta=0.75) #2nd conv. conv2 = Convolution2D(norm1, (29, 29), 96, 256, (5, 5), activation='relu', S=1) # pad=2 - how to do it???? conv2_out = conv2.output() #2nd pooling pool2 = MaxPooling2D(conv2_out, ksize=(1, 3, 3, 1), S=2) pool2_out = pool2.output() # pool2_out = tf.nn.dropout(pool2_out,keep_prob=0.2) norm2 = tf.nn.local_response_normalization(pool2_out, depth_radius=5, alpha=0.0001, beta=0.75) #3rd conv. conv3 = Convolution2D(norm2, (15, 15), 256, 384, (3, 3), activation='relu', S=1) # pad=2 - how to do it???? conv3_out = conv3.output() #3rd pooling pool3 = MaxPooling2D(conv3_out, ksize=(1, 3, 3, 1), S=2) pool3_out = pool3.output() # at this point the representation is (4, 28, 28) i.e. 128*16-dimensional po = tf.reshape(pool3_out, [-1, 384 * 8 * 8]) fc6 = FullConnected(po, 384 * 8 * 8, 512, activation='relu') fc6_out = fc6.output() drop6 = tf.nn.dropout(fc6_out, keep_prob=0.5) fc7 = FullConnected(drop6, 512, 512, activation='relu') fc7_out = fc7.output() drop7 = tf.nn.dropout(fc7_out, keep_prob=0.5) fc8 = FullConnected(drop7, 512, 8, activation='relu') fc8_out = fc8.output() # crossentry for classifier cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=fc8_out) loss = tf.reduce_mean(cross_entropy) # accuracy of the trained model, between 0 (worst) and 1 (best) correct_prediction = tf.equal(tf.argmax(fc8_out, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) return loss, accuracy, fc8_out