Example #1
0
def test(first, second, out):

    data_frame1 = np.expand_dims(imread(first), 0)
    data_frame3 = np.expand_dims(imread(second), 0)

    H = data_frame1.shape[1]
    W = data_frame1.shape[2]

    adatptive_H = int(np.ceil(H / 32.0) * 32.0)
    adatptive_W = int(np.ceil(W / 32.0) * 32.0)

    pad_up = int(np.ceil((adatptive_H - H) / 2.0))
    pad_bot = int(np.floor((adatptive_H - H) / 2.0))
    pad_left = int(np.ceil((adatptive_W - W) / 2.0))
    pad_right = int(np.floor((adatptive_W - W) / 2.0))

    print(str(H) + ', ' + str(W))
    print(str(adatptive_H) + ', ' + str(adatptive_W))

    """Perform test on a trained model."""
    with tf.Graph().as_default():
        # Create input and target placeholder.
        input_placeholder = tf.placeholder(tf.float32, shape=(None, H, W, 6))

        input_pad = tf.pad(input_placeholder, [[0, 0], [pad_up, pad_bot], [pad_left, pad_right], [0, 0]], 'SYMMETRIC')

        edge_vgg_1 = Vgg16(input_pad[:, :, :, :3], reuse=None)
        edge_vgg_3 = Vgg16(input_pad[:, :, :, 3:6], reuse=True)

        edge_1 = tf.nn.sigmoid(edge_vgg_1.fuse)
        edge_3 = tf.nn.sigmoid(edge_vgg_3.fuse)

        edge_1 = tf.reshape(edge_1, [-1, input_pad.get_shape().as_list()[1], input_pad.get_shape().as_list()[2], 1])
        edge_3 = tf.reshape(edge_3, [-1, input_pad.get_shape().as_list()[1], input_pad.get_shape().as_list()[2], 1])

        with tf.variable_scope("Cycle_DVF"):
            # Prepare model.
            model = Voxel_flow_model(is_train=False)
            prediction = model.inference(tf.concat([input_pad, edge_1, edge_3], 3))[0]

        # Create a saver and load.
        sess = tf.Session()

        # Restore checkpoint from file.
        if FLAGS.pretrained_model_checkpoint_path:
            restorer = tf.train.Saver()
            restorer.restore(sess, FLAGS.pretrained_model_checkpoint_path)
            print('%s: Pre-trained model restored from %s' %
                  (datetime.now(), FLAGS.pretrained_model_checkpoint_path))

        feed_dict = {input_placeholder: np.concatenate((data_frame1, data_frame3), 3)}
        # Run single step update.
        prediction_np = sess.run(prediction, feed_dict=feed_dict)

        output = prediction_np[-1, pad_up:adatptive_H - pad_bot, pad_left:adatptive_W - pad_right, :]
        output = np.round(((output + 1.0) * 255.0 / 2.0)).astype(np.uint8)
        output = np.dstack((output[:, :, 2], output[:, :, 1], output[:, :, 0]))
        cv2.imwrite(out, output)
def evaluate(args):
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               size=args.content_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(args.style_image, size=args.style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)

    vgg = Vgg16()
    utils.init_vgg16(args.vgg_model_dir)
    vgg.load_state_dict(
        torch.load(os.path.join(args.vgg_model_dir, "vgg16.weight")))

    style_model = HangSNetV1()
    style_model.load_state_dict(torch.load(args.model))

    if args.cuda:
        style_model.cuda()
        vgg.cuda()
        content_image = content_image.cuda()
        style = style.cuda()

    style_v = Variable(style, volatile=True)
    utils.subtract_imagenet_mean_batch(style_v)
    features_style = vgg(style_v)
    gram_style = [utils.gram_matrix(y) for y in features_style]

    content_image = Variable(utils.preprocess_batch(content_image))
    target = Variable(gram_style[2].data, requires_grad=False)
    style_model.setTarget(target)

    output = style_model(content_image)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Example #3
0
def test(test_data_path):
    with tf.Session() as sess:
        #加载数据
        test_data = np.load(test_data_path)
        test_x = test_data['x']
        test_y = test_data['y']

        x = tf.placeholder(tf.float32, [None, 224, 224, 1])
        y = tf.placeholder(tf.float32, [None, n_classes])

        #构建模型
        model = Vgg16()
        pred = model.build(x)
        #测试网络
        correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
        #初始化所有共享变量
        sess.run(tf.global_variables_initializer())
        #保存模型
        saver = tf.train.Saver()

        print("===============Testing===================")
        saver.restore(sess, 'model/...')
        batch_x = test_x
        batch_y = test_y
        accu = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
        loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
        print('Testing Accuracy= %.5f' % (accu))
def get_vgg_model(MAX_CAPTION_LEN):
    image_model = Vgg16().model
    image_model.pop()
    image_model.pop()
    image_model.trainable = False
    image_model.add(RepeatVector(MAX_CAPTION_LEN))
    return image_model
Example #5
0
    def setup(self, sess, reuse=False):
        try:
            with tf.name_scope('vgg16') as scope:
                # if reuse:
                #     scope.reuse_variables()
                self.edge_model = Vgg16(self.cfgs,
                                        self.img_place_holder,
                                        reuse=reuse)

            from tensorflow.python import pywrap_tensorflow
            reader = pywrap_tensorflow.NewCheckpointReader(
                '/home/yyl/pjs/pycharm-remote-data/HED-BSDS/holy-edge/hed/vgg16/hed-model-5000'
            )
            var_to_shape_map = reader.get_variable_to_shape_map()

            # saver_edge = tf.train.import_meta_graph('/home/yyl/pjs/pycharm-remote-data/HED-BSDS/holy-edge/hed/vgg16/hed-model-5000.meta')

            t_vars = tf.global_variables()
            var_edge = [var for var in t_vars if 'vgg16' in var.name]
            saver_edge = tf.train.Saver(var_edge)
            saver_edge.restore(
                sess,
                '/home/yyl/pjs/pycharm-remote-data/HED-BSDS/holy-edge/hed/vgg16/hed-model-5000'
            )
        except Exception as err:
            self.io.print_error(
                'Error setting up VGG-16 model, {}'.format(err))
            self.init = False
Example #6
0
    def __init__(self, out_channels=2):
        super(ResidualEncoder, self).__init__()
        # using a pretrained model vgg16
        self.resnet = Vgg16()

        # convolution only adjust the width and height size
        # the b_conv4 layer use 1*1 filter
        self.b_conv4 = nn.Conv2d(512, 256, kernel_size=1, stride=1, padding=0)

        # assert(self.bn_4.get_shape().as_list()[1:] == [28, 28, 512])
        # assert(self.b_conv4.get_shape().as_list()[1:] == [28, 28, 256])

        # Backward upscale layer 4
        self.b_conv4_upscale = nn.Upsample(scale_factor=2)
        self.b_conv3 = nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1)

        # Backward upscale layer 3
        self.b_conv3_upscale = nn.Upsample(scale_factor=2)
        self.b_conv2 = nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1)

        # Backward upscale layer 2
        self.b_conv2_upscale = nn.Upsample(scale_factor=2)
        self.b_conv1 = nn.Conv2d(64, 3, kernel_size=3, stride=1, padding=1)

        # Backward upscale bottom layer
        self.b_conv1_upscale = nn.Upsample(scale_factor=2)
        self.b_conv0 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1)

        self.b_conv_last = nn.Conv2d(3,
                                     out_channels,
                                     kernel_size=3,
                                     stride=1,
                                     padding=1)
Example #7
0
def train_ofb(args):
    train_dataset = dataset.DAVISDataset(args.dataset, use_flow=True)
    train_loader = DataLoader(train_dataset, batch_size=1)

    transformer = transformer_net.TransformerNet(args.pad_type)
    transformer.train()
    optimizer = torch.optim.Adam(transformer.parameters(), args.lr)
    mse_loss = torch.nn.MSELoss()

    vgg = Vgg16()
    vgg.load_state_dict(
        torch.load(os.path.join(args.vgg_model_dir, "vgg16.weight")))
    vgg.eval()

    if args.cuda:
        transformer.cuda()
    vgg.cuda()
    mse_loss.cuda()

    style = utils.tensor_load_resize(args.style_image, args.style_size)
    style = style.unsqueeze(0)
    print("=> Style image size: " + str(style.size()))
    print("=> Pixel OFB loss weight: %f" % args.time_strength)

    style = utils.preprocess_batch(style)
    if args.cuda: style = style.cuda()
    style = utils.subtract_imagenet_mean_batch(style)
    features_style = vgg(style)
    gram_style = [utils.gram_matrix(y).detach() for y in features_style]

    train_loader.dataset.reset()
    transformer.train()
    transformer.cuda()
    agg_content_loss = agg_style_loss = agg_pixelofb_loss = 0.
    iters = 0
    anormaly = False
    elapsed_time = 0
    for batch_id, (x, flow, conf) in enumerate(tqdm(train_loader)):
        x, flow, conf = x[0], flow[0], conf[0]
        iters += 1

        optimizer.zero_grad()
        x = utils.preprocess_batch(x)  # (N, 3, 256, 256)
        if args.cuda:
            x = x.cuda()
            flow = flow.cuda()
            conf = conf.cuda()
        y = transformer(x)  # (N, 3, 256, 256)

        begin_time = time.time()
        warped_y, warped_y_mask = warp(y[1:], flow)
        warped_y = warped_y.detach()
        warped_y_mask *= conf
        pixel_ofb_loss = args.time_strength * weighted_mse(
            y[:-1], warped_y, warped_y_mask)
        pixel_ofb_loss.backward()
        elapsed_time += time.time() - begin_time
        if batch_id > 1000: break
    print(elapsed_time / float(batch_id + 1))
Example #8
0
 def conv_model(self):
     vgg = Vgg16()
     model = vgg.model
     last_conv_idx = [
         i for i, l in enumerate(model.layers) if type(l) is Convolution2D
     ][-1]
     conv_layers = model.layers[:last_conv_idx + 1]
     return Sequential(conv_layers)
Example #9
0
    def __init__(self, num_classes):
        self.model_dir = FLAGS.model_path
        self.cnn_net = Vgg16()

        with tf.device("/gpu:0"):
            self.faster_rcnn = FasterRCNN(self.cnn_net, num_classes, batch_size=BATCH_SIZE, is_training=False)
            self.faster_rcnn.build(mode='predict')
        self._initialize()
Example #10
0
def main(argv=None):  # pylint: disable=unused-argument
    X = tf.placeholder(tf.float32, [None, 224, 224, 3])
    y = tf.placeholder(tf.int64, [None])
    is_train = tf.placeholder(tf.bool)
    global_step = tf.Variable(0, trainable=False)

    vgg = Vgg16(model_path=None,
                learning_rate=1e-3,
                decay=0.9999,
                decay_step=350,
                decay_factor=0.1)
    logits = vgg.inference(X, is_train)
    loss = vgg.loss(logits, y)
    train_op = vgg.train(loss, global_step)

    accuracy = vgg.accuracy(logits, y)

    # Create a saver.
    # saver = tf.train.Saver(tf.global_variables())

    # Build the summary operation based on the TF collection of Summaries.
    # summary_op = tf.merge_all_summaries()

    # Build an initialization operation to run below.
    init = tf.global_variables_initializer()

    batch_data = BatchDatset()

    with tf.Session() as sess:
        with tf.device('/gpu:0'):
            sess.run(init)
            losses = []
            accs = []
            print('*************** training ***************')
            for step in xrange(FLAGS.max_iter):
                # create a feed dictionary for this batch
                X_batch, y_batch = batch_data.next_batch(FLAGS.batch_size)
                feed_dict = {
                    X: X_batch,
                    y: y_batch,
                    is_train: FLAGS.mode is 'train'
                }
                _, los, acc, fc8 = sess.run(
                    [train_op, loss, accuracy, vgg.fc8], feed_dict=feed_dict)
                losses.append(los)
                accs.append(acc)
                print('loss:', los)

                if step % FLAGS.print_every == 0:
                    print(
                        "Iteration {0}: with minibatch training loss = {1:.3g} and accuracy of {2:.2g}%"
                        .format(step, los, acc * 100))

    print('losses:', losses)
    print('accs:', accs)
    data = {'loss': losses, 'accuracy': accs}
    with open('temp.pickle', 'wb') as file:
        pickle.dump(data, file)
Example #11
0
def prepare_vgg(sal_type, layer_idx, load_weights, sess):

    # construct the graph based on the gradient type we want
    if sal_type == 'GuidedBackprop':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'GuidedRelu'}):
            vgg = Vgg16(sess=sess)

    elif sal_type == 'Deconv':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'DeconvRelu'}):
            vgg = Vgg16(sess=sess)

    elif sal_type == 'PlainSaliency':
        vgg = Vgg16(sess=sess)

    else:
        raise Exception("Unknown saliency_map type - 1")

    # different options for loading weights
    if load_weights == 'trained':
        vgg.load_weights(weight_path, sess)

    elif load_weights == 'random':
        vgg.init(sess)

    elif load_weights == 'part':
        # fill the first "idx" layers with the trained weights
        # randomly initialize the rest
        vgg.load_weights_part(layer_idx * 2 + 1, weight_path, sess)

    elif load_weights == 'reverse':
        # do not fill the first "idx" layers with the trained weights
        # randomly initialize them
        vgg.load_weights_reverse(layer_idx * 2 + 1, weight_path, sess)

    elif load_weights == 'only':
        # do not load a specific layer ("idx") with the trained weights
        # randomly initialize it
        vgg.load_weights_only(layer_idx * 2 + 1, weight_path, sess)

    else:
        raise Exception("Unknown load_weights type - 1")

    return vgg
Example #12
0
def define_VGG(pretrained=True, gpu_ids=[]):
    vgg = Vgg16()

    if pretrained:
        vgg.load_state_dict(torch.load('models/vgg16.weight'))

    if len(gpu_ids) > 0:
        vgg.cuda(device_id=gpu_ids[0])
    return vgg
def init_vgg16(model_folder):
    if not os.path.exists(os.path.join(model_folder, 'vgg16.weight')):
        if not os.path.exists(os.path.join(model_folder, 'vgg16.t7')):
            os.system(
                'wget https://www.dropbox.com/s/76l3rt4kyi3s8x7/vgg16.t7?dl=1 -O ' + os.path.join(model_folder, 'vgg16.t7'))
        vgglua = load_lua(os.path.join(model_folder, 'vgg16.t7'))
        vgg = Vgg16()
        for (src, dst) in zip(vgglua.parameters()[0], vgg.parameters()):
            dst.data[:] = src
        torch.save(vgg.state_dict(), os.path.join(model_folder, 'vgg16.weight'))
Example #14
0
def prepare_vgg(sal_type, sess):
    # construct the graph based on the gradient type we want
    if sal_type == 'GuidedBackprop':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'GuidedRelu'}):
            vgg = Vgg16(sess=sess)

    elif sal_type == 'Deconv':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'DeconvRelu'}):
            vgg = Vgg16(sess=sess)

    elif sal_type == 'PlainSaliency':
        vgg = Vgg16(sess=sess)

    else:
        raise Exception("Unknown saliency type")

    return vgg
Example #15
0
def optimize(args):
	"""	Gatys et al. CVPR 2017
	ref: Image Style Transfer Using Convolutional Neural Networks
	"""
	# load the content and style target
	content_image = utils.tensor_load_rgbimage(args.content_image, size=args.content_size, keep_asp=True)
	content_image = content_image.unsqueeze(0)
	content_image = Variable(utils.preprocess_batch(content_image), requires_grad=False)
	utils.subtract_imagenet_mean_batch(content_image)
	style_image = utils.tensor_load_rgbimage(args.style_image, size=args.style_size)
	style_image = style_image.unsqueeze(0)	
	style_image = Variable(utils.preprocess_batch(style_image), requires_grad=False)
	utils.subtract_imagenet_mean_batch(style_image)

	# load the pre-trained vgg-16 and extract features
	vgg = Vgg16()
	utils.init_vgg16(args.vgg_model_dir)
	vgg.load_state_dict(torch.load(os.path.join(args.vgg_model_dir, "vgg16.weight")))
	if args.cuda:
		content_image = content_image.cuda()
		style_image = style_image.cuda()
		vgg.cuda()
	features_content = vgg(content_image)
	f_xc_c = Variable(features_content[1].data, requires_grad=False)
	features_style = vgg(style_image)
	gram_style = [utils.gram_matrix(y) for y in features_style]
	# init optimizer
	output = Variable(content_image.data, requires_grad=True)
	optimizer = Adam([output], lr=args.lr)
	mse_loss = torch.nn.MSELoss()
	# optimizing the images
	for e in range(args.iters):
		utils.add_imagenet_mean_batch(output)
		output.data.clamp_(0, 255)	
		utils.subtract_imagenet_mean_batch(output)

		optimizer.zero_grad()
		features_y = vgg(output)
		content_loss = args.content_weight * mse_loss(features_y[1], f_xc_c)

		style_loss = 0.
		for m in range(len(features_y)):
			gram_y = utils.gram_matrix(features_y[m])
			gram_s = Variable(gram_style[m].data, requires_grad=False)
			style_loss += args.style_weight * mse_loss(gram_y, gram_s)

		total_loss = content_loss + style_loss

		if (e + 1) % args.log_interval == 0:
			print(total_loss.data.cpu().numpy()[0])
		total_loss.backward()
		
		optimizer.step()
	# save the image	
	utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Example #16
0
    def build_net(self, image, word, target_word, config):
        with tf.name_scope("content_vgg"):
            self.cnn_net = Vgg16()
            self.cnn_net.build(image)

        with tf.device("/gpu:0"):
            init_state = self.cnn_net.relu6
            self.lstm = lstm.LSTM(initial_state=init_state,
                                  input=word,
                                  target=target_word,
                                  config=config)
Example #17
0
def main(FLAGS):

    v = Vgg16()
    # var_list = [v for k, v in v.var_dict.items()]
    checkpoint = tf.train.Checkpoint(model=v)
    checkpoint.restore(FLAGS.start_checkpoint)
    call = v.__call__.get_concrete_function(
        tf.TensorSpec([None, 224, 224, 3], tf.float32))
    tf.saved_model.save(v,
                        "/home/applevidur/tf_model/vgg16_with_signature/1",
                        signatures=call)
Example #18
0
def init_vgg16(model_folder):
    """load the vgg16 model feature"""
    if not os.path.exists(os.path.join(model_folder, 'vgg16.weight')):
        if not os.path.exists(os.path.join(model_folder, 'vgg16.t7')):
            os.system(
                'wget http://cs.stanford.edu/people/jcjohns/fast-neural-style/models/vgg16.t7 -O ' + os.path.join(model_folder, 'vgg16.t7'))
        vgglua = load_lua(os.path.join(model_folder, 'vgg16.t7'))
        vgg = Vgg16()
        for (src, dst) in zip(vgglua.parameters()[0], vgg.parameters()):
            dst.data[:] = src
        torch.save(vgg.state_dict(), os.path.join(model_folder, 'vgg16.weight'))
Example #19
0
def init_vgg16(model_folder):
    from vgg16 import Vgg16
    if not os.path.exists(os.path.join(model_folder, 'vgg16.weight')):
        if not os.path.exists(os.path.join(model_folder, 'vgg16.t7')):
            os.system(
                'wget https://www.dropbox.com/s/76l3rt4kyi3s8x7/vgg16.t7?dl=1 -O '
                + os.path.join(model_folder, 'vgg16.t7'))
        raise Exception("fix lua->python loading since api was removed")
        vgglua = load_lua(os.path.join(model_folder, 'vgg16.t7'))
        vgg = Vgg16()
        for (src, dst) in zip(vgglua.parameters()[0], vgg.parameters()):
            dst.data[:] = src
Example #20
0
    def __init__(self, training_data_provider, dropout=0.6, batch_size=64):
        vgg = Vgg16()
        model = vgg.model

        conv_model, _ = split_model(model)
        dense_model = get_batchnorm_model(conv_model, dropout)

        self._conv_model = conv_model
        self._dense_model = dense_model
        self._batch_size = batch_size
        self._train_feature_set = training_data_provider.get_feature_set(
            'train', conv_model, 'sf_train_conv_features.bc')
        self._train_feature_set.shuffle()
        self._valid_feature_set = training_data_provider.get_feature_set(
            'valid', conv_model, 'sf_valid_conv_features.bc')
Example #21
0
    def __init__(self, net_name, dataset_name, pre_model=None, is_train=True):
        super().__init__()
        self.net_name = net_name
        self.is_train = is_train

        if dataset_name == 'sysu':
            self.num_pid = 5532
            self.queue_size = 5000
        elif dataset_name == 'prw':
            self.num_pid = 483
            self.queue_size = 500
        else:
            raise KeyError(dataset_name)
        self.lut_momentum = 0.5
        self.reid_feat_dim = 256

        self.register_buffer(
            'lut',
            torch.zeros(self.num_pid, self.reid_feat_dim).cuda())
        self.register_buffer(
            'queue',
            torch.zeros(self.queue_size, self.reid_feat_dim).cuda())

        if self.net_name == 'vgg16':
            self.net = Vgg16(pre_model, self.is_train)
        elif self.net_name == 'res34':
            self.net = resnet(34, pre_model, self.is_train)
        elif self.net_name == 'res50':
            self.net = resnet(50, pre_model, self.is_train)
        elif self.net_name == 'dense121':
            self.net = densenet(121, pre_model, self.is_train)
        elif self.net_name == 'dense161':
            self.net = densenet(161, pre_model, self.is_train)
        else:
            raise KeyError(self.net_name)

        self.fc7_channels = self.net.fc7_channels

        # SPIN consists of three main parts
        self.head = self.net.head
        self.strpn = STRPN(self.net.net_conv_channels, self.num_pid,
                           self.is_train)
        self.tail = self.net.tail

        self.cls_score_net = nn.Linear(self.fc7_channels, 2)
        self.bbox_pred_net = nn.Linear(self.fc7_channels, 8)
        self.reid_feat_net = nn.Linear(self.fc7_channels, self.reid_feat_dim)
        self.init_linear_weight(False)
Example #22
0
def create_model(opt):
    vgg = Vgg16()
    vgg.model.pop()

    click.echo('Replacing last layer of model...')
    for layer in vgg.model.layers:
        layer.trainable = False
    vgg.model.add(Dense(2, activation='softmax'))

    vgg.model.compile(optimizer=opt,
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

    vgg.model.load_weights(os.path.join(MODEL_PATH, 'finetune_1_ll.h5'))

    return vgg
Example #23
0
def create_model():
    vgg = Vgg16()
    vgg.model.pop()

    click.echo('Replacing last layer of model...')
    for layer in vgg.model.layers:
        layer.trainable = False
    vgg.model.add(Dense(2, activation='softmax'))

    # OPTIMIZER = Nadam()
    OPTIMIZER = RMSprop(lr=0.001)
    vgg.model.compile(optimizer=OPTIMIZER,
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

    return vgg, OPTIMIZER
Example #24
0
def predict_imagenet(filename):
    test_x = ImageDataGenerator.get_image(filename)
    X = tf.placeholder(tf.float32, [None, 224, 224, 3])
    keep_prob = tf.placeholder(tf.float32)
    model = Vgg16(X, keep_prob)
    y_ = model.create_model()
    prob = tf.nn.softmax(y_)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        model.initial_weights(sess)
        y_, output = sess.run([y_, prob],
                              feed_dict={
                                  X: [test_x],
                                  keep_prob: 1
                              })
        ImageDataGenerator.predict(output)
Example #25
0
    def __init__(self, imdb, roidb, val_imdb, val_roidb, pretrain_model):
        self.imdb = imdb
        self.roidb = roidb
        self.val_imdb = val_imdb
        self.val_roidb = val_roidb
        self.pretrain_model = pretrain_model
        self.model_dir = FLAGS.model_path
        self.log_dir = FLAGS.log_path
        self.val_log_dir = FLAGS.val_log_path

        self.cnn_net = Vgg16()
        with tf.device("/gpu:0"):
            self.faster_rcnn = FasterRCNN(self.cnn_net, self.imdb.num_classes, batch_size=config.BATCH_SIZE, is_training=True)
            self.faster_rcnn.build(mode='train')

        variables = tf.global_variables()
        print ("all var:",variables)
def main():
    #taken care of in ~/.keras/keras.json
    # from keras import backend
    # backend.set_image_dim_ordering('tf')

    #which dataset to work on?large or the small
    # path = settings.DATA_ALL
    path = settings.DATA_SAMPLE

    # where are we?
    datadir = os.path.join(os.getcwd(), settings.DATA_FOLDER_NAME)

    # create model with correct number of outputs
    # this pops off the existing last layer, sets remaining layers to not trainable
    # and puts on a layer with the correct number of outputs
    # Import our class, and instantiate
    from vgg16 import Vgg16
    vgg = Vgg16()

    # Grab a few images at a time for training and validation.
    # NB: They must be in subdirectories named based on their category
    batches = vgg.get_batches(os.path.join(path, settings.TRAIN_FOLDER_NAME),
                              batch_size=settings.BATCH_SIZE)
    val_batches = vgg.get_batches(os.path.join(path,
                                               settings.VALIDATE_FOLDER_NAME),
                                  batch_size=settings.BATCH_SIZE * 2)

    # add a different fully connected layer at the end
    # load the best weights from CHECKPOINTFILE_1 (if it exists)
    vgg.finetune(batches, checkpointfile=settings.CHECKPOINTFILE_1)

    # create checkpoint to be used by model.fit to save the best model via callback
    filepath = settings.CHECKPOINTFILE_1
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_acc',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='max')
    callbacks_list = [checkpoint]

    # fit the model
    vgg.fit(batches,
            val_batches,
            nb_epoch=settings.NUMBER_EPOCHS,
            callbacks=callbacks_list)
Example #27
0
def predict_dog_cat(filename):
    test_x = ImageDataGenerator.get_image(filename)
    X = tf.placeholder(tf.float32, [None, 224, 224, 3])
    keep_prob = tf.placeholder(tf.float32)
    model = Vgg16(X, keep_prob, 2)
    y_ = model.create_model()
    predict = tf.argmax(y_, 1)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state('./ckpt')
        if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
            print("Reading model parameters from %s" %
                  ckpt.model_checkpoint_path)
            saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            print("no model")
            return
        pred = sess.run(predict, feed_dict={X: [test_x], keep_prob: 1})
        print(cat_dog_class_names[pred[0]])
Example #28
0
    def encoder(self, x, is_training=True, reuse=False):
        vgg_net = Vgg16()
        vgg_net.encoder(x)
        with tf.variable_scope(self.scope, reuse=False):
            self.conv6 = self.conv_layer(vgg_net.conv5_3, [3, 3], self.channel_scale(512), self.channel_scale(1024), is_training, scope="conv6")
            self.conv7 = self.conv_layer(self.conv6, [1, 1], self.channel_scale(1024), self.channel_scale(1024), is_training, scope="conv7")

            self.conv8_1 = self.conv_layer(self.conv7, [1, 1], self.channel_scale(1024), self.channel_scale(256), is_training, scope="conv8_1")
            self.conv8_2 = self.conv_layer(self.conv8_1, [3, 3], self.channel_scale(256), self.channel_scale(512), is_training, scope="conv8_2", stride=2)

            self.conv9_1 = self.conv_layer(self.conv8_2, [1, 1], self.channel_scale(512), self.channel_scale(128), is_training, scope="conv9_1")
            self.conv9_2 = self.conv_layer(self.conv9_1, [3, 3], self.channel_scale(128), self.channel_scale(256), is_training, scope="conv9_2",stride=2)

            self.conv10_1 = self.conv_layer(self.conv9_2, [1, 1], self.channel_scale(256), self.channel_scale(128), is_training, scope="conv10_1")
            self.conv10_2 = self.conv_layer(self.conv10_1, [3, 3], self.channel_scale(128), self.channel_scale(256), is_training, scope="conv10_2", stride=2)

            self.p11 = tf.nn.avg_pool(self.conv10_2, [1, 3, 3, 1], [1, 1, 1, 1], "SAME")

            all_class_num = self.class_num + 1

            self.out1 = self.conv_layer(vgg_net.conv4_3, [3, 3], self.channel_scale(512), self.layer_boxs[0]*(all_class_num + 4), is_training, scope="out1",
                                   activation_fn=None)
            self.out2 = self.conv_layer(self.conv7, [3, 3], self.channel_scale(1024), self.layer_boxs[1]* (all_class_num + 4), is_training, scope="out2",
                                   activation_fn=None)
            self.out3 = self.conv_layer(self.conv8_2, [3, 3], self.channel_scale(512), self.layer_boxs[2]* (all_class_num + 4), is_training, scope="out3",
                                   activation_fn=None)
            self.out4 = self.conv_layer(self.conv9_2, [3, 3], self.channel_scale(256), self.layer_boxs[3]* (all_class_num + 4), is_training, scope="out4",
                                   activation_fn=None)
            self.out5 = self.conv_layer(self.conv10_2, [3, 3], self.channel_scale(256), self.layer_boxs[4]* (all_class_num + 4), is_training, scope="out5",
                                   activation_fn=None)
            self.out6 = self.conv_layer(self.p11, [3, 3], self.channel_scale(256), self.layer_boxs[5]* (all_class_num + 4), is_training, scope="out6",
                                   activation_fn=None)

            self.outs = [self.out1, self.out2, self.out3, self.out4, self.out5, self.out6]
            self.outfs = []
            for i, out in enumerate(self.outs):
                tmp_outf = tf.reshape(out, [self.flags.batch_size, -1, all_class_num+4])
                self.outfs.append(tmp_outf)
            self.formatted_outs = tf.concat(self.outfs, 1)
            self.pred_labels = self.formatted_outs[:, :, :all_class_num]
            self.pred_locs = self.formatted_outs[:, :, all_class_num:]
Example #29
0
def get_vgg(path_config, batch_size):
    """Tune the model and return model"""

    print("Getting model for {p}".format(p=path_config.data))

    vgg = Vgg16()

    existing_weight_file = path_config.get_latest_weight()

    if os.path.exists(existing_weight_file):
        print("Loading weights from {wf}".format(wf=existing_weight_file))
        vgg.model.load_weights(existing_weight_file)
    else:
        # Grab a few images at a time for training and validation.
        # NB: They must be in subdirectories named based on their category
        train_batches = vgg.get_batches(path_config.train, batch_size=batch_size)
        val_batches = vgg.get_batches(path_config.valid, batch_size=batch_size*2)

        vgg.finetune(train_batches)

        vgg.model.optimizer.lr = 0.01
        print("Learning Rate={lr}, Batch size={bs}".format(lr=vgg.model.optimizer.lr, bs=batch_size))

        timestamp_as_string = datetime.now().strftime("%Y-%m-%d-%H%M%S")

        epoch_count = 10
        epoch_history = []
        for epoch in range(epoch_count):
            print("Fitting epoch {c}/{of}".format(c=epoch, of=epoch_count))
            history = vgg.fit(train_batches, val_batches, nb_epoch=1)

            weights_filename = "{ts}_fit_{epoch}.h5".format(ts=timestamp_as_string, epoch=epoch)
            weights_file_path = os.path.join(path_config.results, weights_filename)
            #print("Saving weights to {weights_file}".format(weights_file=weights_file_path))
            vgg.model.save_weights(weights_file_path)
            epoch_history.append(history.history)

            
        print(epoch_history)

    return vgg
Example #30
0
def train_vgg(img_type):
    #data
    train_imgs, train_labels = read_image(path, img_type)

    #model
    vgg = Vgg16()
    images = tf.placeholder(tf.float32, [None, 256, 256, 1])
    labels = tf.placeholder(tf.int32, [None, 1])

    probs, logits = vgg.build(images)
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
    train_step = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(loss)
    correct_prediction = tf.equal(tf.cast(tf.argmax(logits, 1), tf.int32),
                                  labels)
    acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    saver = tf.train.Saver()

    #training part
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(1, max_epochs + 1):
            start_time = time.time()
            #training
            for batch_images, batch_labels in minibatches(train_imgs,
                                                          train_labels,
                                                          batch_size,
                                                          shuffle=True):

                feed_dict = {labels: batch_labels, images: batch_images}
                _, err, ac = sess.run([train_step, loss, acc],
                                      feed_dict=feed_dict)

            end_time = time.time()
            print(end_time - start_time)

            if epoch % 500 == 0:
                saver.save(sess, './model' + str(epoch) + '.ckpt')