def create_model(self, img_shape, num_class): self.handle_dim_ordering() K.set_learning_phase(True) #model = models.Sequential() inputs = layers.Input(shape = img_shape) x = self.conv_bn_relu(inputs, (3, 3),(1, 1), 32, 'conv0_1') net = self.conv_bn_relu(x, (3, 3), (1, 1), 64, 'conv0_2') bn1 = layers.BatchNormalization(momentum=0.99, name='conv0_3_bn')(self.conv( net, (3, 3), (1, 1), 32, 'conv0_3')) act1 = layers.Activation('relu')(bn1) bn2, act2 = self.down_block(act1, 32, 'down1') bn3, act3 = self.down_block(act2, 32, 'down2') bn4, act4 = self.down_block(act3, 32, 'down3') bn5, act5 = self.down_block(act4, 32, 'down4') bn6, act6 = self.down_block(act5, 32, 'down5') bn7, act7 = self.down_block(act6, 32, 'down6') temp = self.up_block(act7, bn6, 32, 'up6') temp = self.up_block(temp, bn5, 32, 'up5') temp = self.up_block(temp, bn4, 32, 'up4') temp = self.up_block(temp, bn3, 32, 'up3') temp = self.up_block(temp, bn2, 32, 'up2') temp = self.up_block(temp, bn1, 32, 'up1') output = self.conv(temp, (1, 1), (1, 1), num_class, 'output') model = models.Model(outputs=output, inputs=inputs) print(model.summary()) return model
def predict(path, imgs, builder): K.set_learning_phase(False) model = builder(learning=False) model.load_weights(path) print(model.summary()) @timeit def inner(imgs): return model.predict(imgs) return inner(imgs)
def constantize(fname): K.clear_session() tf.reset_default_graph() K.set_learning_phase(False) mod = models.load_model(fname) outputs = mod.output if not isinstance(outputs, collections.Sequence): outputs = [outputs] output_names = [] for output in outputs: output_names.append(output.name.split(':')[0]) sess = K.get_session() cg = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(add_shapes=True), output_names) K.clear_session() return cg
def compile_keras(train_samples: pd.DataFrame, validation_samples:pd.DataFrame): K.set_learning_phase(True) size = 128 steps_per_epoch = len(train_samples) // size validation_steps = len(validation_samples) // size train_generator = generator(train_samples, crop=False)(size, infinite=True) validation_generator = generator(validation_samples, crop=False)(size, infinite=True) model = layers(keep_prob=0.5, input_shape=(200, 300, 3), add_softmax=True) model.compile(loss='categorical_crossentropy', optimizer='adam') tensorboard = keras_tb(log_dir="./logs/{}".format(time.time())) history_object = model.fit_generator(train_generator, # samples_per_epoch=6 * len(train_samples), validation_data=validation_generator, # nb_val_samples=6 * len(validation_samples), epochs=10, callbacks=[tensorboard], validation_steps=validation_steps, steps_per_epoch=steps_per_epoch) model.save_weights('cnn.h5') print(history_object.history.keys()) print('Loss') print(history_object.history['loss']) print('Validation Loss') print(history_object.history['val_loss'])
def fcn(num_classes=2, learning=True) -> Sequential: K.set_learning_phase(learning) reg = tf.contrib.layers.l2_regularizer(1e-3) kernel_size = 3 pad = 'same' act2 = 'relu' model = Sequential() model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(200, 400, 3))) model.add( Conv2D(32, kernel_size, 1, padding=pad, activation=act2, kernel_regularizer=reg)) model.add( Conv2D(32, kernel_size, 1, padding=pad, activation=act2, kernel_regularizer=reg)) model.add(MaxPooling2D((2, 2), 2)) model.add( Conv2D(64, kernel_size, 1, padding=pad, activation=act2, kernel_regularizer=reg)) model.add( Conv2D(64, kernel_size, 1, padding=pad, activation=act2, kernel_regularizer=reg)) model.add(MaxPooling2D((2, 2), 2)) model.add( Conv2D(128, kernel_size, 1, padding=pad, activation=act2, kernel_regularizer=reg)) model.add( Conv2D(128, kernel_size, 1, padding=pad, activation=act2, kernel_regularizer=reg)) model.add(MaxPooling2D((2, 2), 2)) model.add(Conv2D(num_classes, 1, 1, padding=pad, kernel_regularizer=reg)) model.add( Conv2DTranspose(num_classes, kernel_size, strides=2, activation=act2, padding=pad)) model.add(BatchNormalization()) model.add( Conv2DTranspose(num_classes, kernel_size, strides=2, activation=act2, padding=pad)) model.add(BatchNormalization()) model.add( Conv2DTranspose(num_classes, kernel_size, strides=2, activation=act2, padding=pad, name=LOGITS)) model.add(Reshape((-1, num_classes))) return model
def main(_): # disable all training specific operations K.set_learning_phase(0) model = applications.inception_v3.InceptionV3(weights='imagenet', include_top=False) layer_contributions = { 'mixed2': 0.2, 'mixed3': 3.0, 'mixed4': 2.0, 'mixed5': 1.5 } layer_dict = dict([(layer.name, layer) for layer in model.layers]) loss = K.variable(0.,) for layer_name in layer_contributions: coeff = layer_contributions[layer_name] activation = layer_dict[layer_name].output scaling = K.prod(K.cast(K.shape(activation), 'float32')) # avoid artifacts by only involving non-boarder pixels loss += coeff * K.sum(K.square(activation[:, 2:-2, 2:-2, :])) / scaling # start the gradient-ascent process dream = model.input grads_list = K.gradients(loss, dream) grads = grads_list[0] # trick: normalize gradients grads /= K.maximum(K.mean(K.abs(grads)), 1e-7) fetch_loss_and_grads = K.function(inputs=[dream], outputs=[loss, grads]) def gradient_ascent(x, iterations, step_rate, max_loss=None): for i in range(iterations): loss_value, grads_value = fetch_loss_and_grads([x]) if max_loss is not None and loss_value > max_loss: break print('@{:4d}: {:.4f}'.format(i, loss_value)) x += step_rate * grads_value return x img = preprocess_img(FLAGS.img_path) original_shape = img.shape[1:3] successive_shapes = [original_shape] for i in range(1, NUM_OCTAVES): shape = tuple([int(dim / (OCTAVES_SCLAE ** i)) for dim in original_shape]) successive_shapes.append(shape) # reverse successive_shapes = successive_shapes[::-1] original_img = np.copy(img) shrunk_original_img = resize_img(img, successive_shapes[0]) for shape in successive_shapes: print('Preprocess image with shape: {}'.format(shape)) img = resize_img(img, shape) img = gradient_ascent(img, iterations=FLAGS.iterations, step_rate=FLAGS.step_rate, max_loss=MAX_LOSS) same_size_original = resize_img(original_img, shape) if FLAGS.repair_lost_detail: upscale_shrunk_original_img = resize_img(shrunk_original_img, shape) lost_detail = same_size_original - upscale_shrunk_original_img img += lost_detail shrunk_original_img = same_size_original save_img(img, filename='dream_at_scale_{}.png'.format(str(shape))) save_img(img, filename='dream.png')