def load_model(): text_model = DenseNet(classes=n_classes, input_shape=image_shape, depth=40, growth_rate=12, bottleneck=True, reduction=0.5, dropout_rate=0.0, weight_decay=1e-4) text_model.load_weights(text_model_weight) return text_model
base_model = DenseNet(classes=n_classes, input_shape=image_shape, depth=args.depth, growth_rate=args.growth_rate, bottleneck=args.bc_mode, reduction=args.reduction, dropout_rate=1.0 - args.keep_prob, weight_decay=args.weight_decay) if args.train: batch_size *= n_gpus if os.path.exists("saves/%s.weight" % model_identifier): print("Loading model...") base_model.load_weights("saves/%s.weight" % model_identifier, by_name=True) if n_gpus > 1: model = multi_gpu_model(base_model, n_gpus) else: model = base_model def loss_func(y_true, y_pred): l2_loss = tf.add_n( [tf.nn.l2_loss(var) for var in model.trainable_weights]) return categorical_crossentropy(y_true, y_pred) + l2_loss * 1e-4 optimizer = SGD(lr=initial_learning_rate, clipvalue=0.5, momentum=0.9, decay=1e-4,
# Start Tensorflow session with tf.Session() as sess: # Initialize all variables ckpt = tf.train.get_checkpoint_state(checkpoint_path) if ckpt and ckpt.model_checkpoint_path: pass else: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) # Load the pretrained weights into the non-trainable layer if pretrain and mode == 'alexnet': model.load_initial_weights(sess) elif pretrain and mode == 'densenet': model_op.load_weights() elif pretrain and mode == 'resnet': raise ValueError("Invalid pretrain for resnet") if ckpt and ckpt.model_checkpoint_path: print('Training from saved model ...') saver.restore(sess, ckpt.model_checkpoint_path) graph = tf.get_default_graph() # Add the model graph to TensorBoard writer.add_graph(sess.graph) print("{} Start training...".format(datetime.now())) print("{} Open Tensorboard at --logdir {}".format(datetime.now(), filewriter_path))