Exemple #1
0
def main():
    basepath = "test_data"
    bakpath = "bakvgg16"
    mkdir(bakpath + "/" + basepath)
    imgdata = ["./test_data/tiger.jpeg"]
    imgdata = loadFrom(basepath)  #20180330
    num = len(imgdata)
    if num == 0:
        utils.printcolor("图像文件数量为0", mode='bold', fore='red')
        return

    per = 10 if (num > 10) else num
    count = int(num / per) if (num % per == 0) else int(num / per) + 1
    print(per, num, count, num % per)

    vgg = vgg16.Vgg16()
    images = tf.placeholder("float", [per, 224, 224, 3])
    with tf.name_scope("content_vgg"):
        vgg.build(images)

    for x in range(0, count):
        xdata = imgdata[x * per:x * per + per]
        #print(len(xdata))
        if len(xdata) == num % per:
            vggx = vgg16.Vgg16()
            images = tf.placeholder("float", [len(xdata), 224, 224, 3])
            with tf.name_scope("content_vgg"):
                vggx.build(images)
            tensor_imgdata(xdata, images, vggx, bakpath)
        else:
            tensor_imgdata(xdata, images, vgg, bakpath)
Exemple #2
0
def VGG16_Synthesis_Network(input_img, input_Semantic):
	# img encoder
	vgg = vgg16.Vgg16()
	vgg.build(input_img)
	img_layer = tf.contrib.layers.flatten(vgg.pool5)
	img_layer = tf.contrib.layers.fully_connected(img_layer, 512)

	# Semantic encoder
	Semantic_layer = tf.contrib.layers.fully_connected(input_Semantic, 512)

	# Semantic synthesis
	z = tf.concat([img_layer, Semantic_layer], -1)

	# img decoder
	z = tf.expand_dims(z,1)
	z = tf.expand_dims(z,1)
	# output_1 = tf.layers.conv2d_transpose(inputs = z, filters = 512, kernel_size = 3, strides = (2,2), padding='same', data_format='channels_last', activation = tf.nn.relu)
	# output_2 = tf.layers.conv2d_transpose(inputs = output_1, filters = 256, kernel_size = 3, strides = (2,2), padding='same', data_format='channels_last', activation = tf.nn.relu)
	# output_3 = tf.layers.conv2d_transpose(inputs = output_2, filters = 256, kernel_size = 3, strides = (2,2), padding='same', data_format='channels_last', activation = tf.nn.relu)
	# output_4 = tf.layers.conv2d_transpose(inputs = output_3, filters = 128, kernel_size = 3, strides = (2,2), padding='same', data_format='channels_last', activation = tf.nn.relu)
	# output_5 = tf.layers.conv2d_transpose(inputs = output_4, filters = 64, kernel_size = 3, strides = (2,2), padding='same', data_format='channels_last', activation = tf.nn.relu)
	# output_6 = tf.layers.conv2d_transpose(inputs = output_5, filters = 32, kernel_size = 3, strides = (2,2), padding='same', data_format='channels_last', activation = tf.nn.relu)
	# output_7 = tf.layers.conv2d_transpose(inputs = output_6, filters = 3, kernel_size = 3, strides = (2,2), padding='same', data_format='channels_last', activation = tf.nn.relu)
	output_1 = residual_block(inputs_layer = z, nb_blocks = 1, out_filters = 512, strides = 2)
	output_2 = residual_block(inputs_layer = output_1, nb_blocks = 1, out_filters = 256, strides = 2)
	output_3 = residual_block(inputs_layer = output_2, nb_blocks = 1, out_filters = 256, strides = 2)
	output_4 = residual_block(inputs_layer = output_3, nb_blocks = 1, out_filters = 128, strides = 2)
	output_5 = residual_block(inputs_layer = output_4, nb_blocks = 1, out_filters = 64, strides = 2)
	output_6 = residual_block(inputs_layer = output_5, nb_blocks = 1, out_filters = 32, strides = 2)
	output_7 = residual_block(inputs_layer = output_6, nb_blocks = 1, out_filters = 3, strides = 2)

	# output
	output_7 = tf.image.resize_images(output_7, [224,224])
	return output_7
    def run(self):

        #set enviornment
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = str(self._gpuid)

        #load models
        import wingdbstub
        wingdbstub.Ensure()
        wingdbstub.debugger.StartDebug()
        import vgg16
        #download the vgg16 weights from
        #https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5
        xnet = vgg16.Vgg16(
            '/media/zhaoke/b0685ee4-63e3-4691-ae02-feceacff6996/models/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
        )

        print 'vggnet init done', self._gpuid

        while True:
            xfile = self._queue.get()
            if xfile == None:
                self._queue.put(None)
                break
            label = self.predict(xnet, xfile)
            print 'woker', self._gpuid, ' xfile ', xfile, " predicted as label", label

        print 'vggnet done ', self._gpuid
        wingdbstub.debugger.StopDebug()
Exemple #4
0
def detect_image_vgg16(request):
    if request.method == "POST":  # 请求方法为POST时,进行处理

        myFile = request.FILES.get("myfile", None)  # 获取上传的文件,如果没有文件,则默认为None
        if not myFile:
            return HttpResponse("no files for upload!")
        img_ready = utils.load_image(myFile)
        with tf.Session() as sess:
            # 给输入图片占位
            images = tf.placeholder(tf.float32, [1, 224, 224, 3])
            # 初始化vgg,读出保存的文件参数
            vgg = vgg16.Vgg16()
            # 调用前向传播函数,把待识别图片喂入神经网络
            vgg.forward(images)
            # 运行会话,预测分类结果
            probability = sess.run(vgg.prob, feed_dict={images: img_ready})
            top5 = np.argsort(probability[0])[-1:-6:-1]
            data = ""
            data += "top5:"
            data += str(top5)
            data += "<br/>"
            for n, i in enumerate(top5):
                data += str(i)
                data += ":"
                data += labels[i]
                data += "----"
                data += str(utils.percent(probability[0][i]))
                data += "<br/>"

        return HttpResponse(
            "<html><center><h1>VGG16物体分类结果展示</h1><body>%s</body></center></html>"
            % data)
Exemple #5
0
    def __init__(self, model_vgg_path = config.MODEL_VGG_PATH,\
                       model_dnn_path = config.MODEL_DNN_PATH, \
                       uid_path = config.UID_PATH):
        lr = 1e-4
        feature_columns = [
            tf.contrib.layers.real_valued_column("", dimension=4)
        ]

        self.classifier = learn.DNNClassifier(
            model_dir=model_dnn_path,
            feature_columns=feature_columns,
            hidden_units=[4096, 2048],
            n_classes=69,
            activation_fn=tf.nn.sigmoid,
            optimizer=tf.train.AdamOptimizer(learning_rate=lr))

        self.sess = tf.Session()
        self.vgg = vgg16.Vgg16(model_vgg_path)
        self.images = tf.placeholder("float", [None, 224, 224, 3])
        with tf.name_scope("content_vgg"):
            self.vgg.build(self.images)

        self.uid = []
        with open(uid_path, 'rt') as f:
            for it in f:
                self.uid.append(it.replace('\n', ''))
def main():
    with tf.Session() as sess:
        input_tensor = tf.placeholder("float", [32, 224, 224, 3])
        vgg = vgg16.Vgg16()
        with tf.name_scope("content_vgg"):
            vgg.build(input_tensor)
        # a loop for all folders
        folders = os.listdir(root_folder)
        folders.sort()
        try:
            start = int(sys.argv[1])
            end = int(sys.argv[2])
        except:
            start = 0
            end = len(folders)
        for folder in folders[start:end]:
            folder_path = os.path.join(root_folder, folder)
            prob_path = os.path.join(probs_folder, folder + '.txt')
            if os.path.exists(prob_path):
                continue
            probs_for_folder = get_1365_vec(vgg, input_tensor, sess,
                                            folder_path)
            try:
                np.savetxt(prob_path, probs_for_folder)
            except Exception as e:
                print(e)
                file = open('error.txt', 'a')
                file.write(folder_path)
                file.close()
    def __init__(self):
        self.vgg = vgg16.Vgg16()

        self.input_holder = tf.placeholder(tf.float32,
                                           [1, img_size, img_size, 3])
        self.label_holder = tf.placeholder(tf.float32,
                                           [label_size * label_size, 2])
        self.S11_holder = tf.placeholder(
            tf.float32,
            [3 * 3 * int(np.floor(cfg.B / 2)),
             int(np.floor(cfg.C / 2))])
        self.S12_holder = tf.placeholder(
            tf.float32,
            [3 * 3 * int(np.floor(cfg.B / 2)),
             int(np.floor(cfg.C / 2))])
        self.S21_holder = tf.placeholder(
            tf.float32,
            [3 * 3 * int(np.floor(cfg.C / 2)),
             int(np.floor(cfg.D / 2))])
        self.S22_holder = tf.placeholder(
            tf.float32,
            [3 * 3 * int(np.floor(cfg.C / 2)),
             int(np.floor(cfg.D / 2))])

        self.sobel_fx, self.sobel_fy = self.sobel_filter()

        self.contour_th = 1.5
        self.contour_weight = 0.0001
def main():
    num_frames = 80
    video_path = './data'
    video_save_path = './data_features'
    videos = os.listdir(video_path)
    videos = filter(lambda x: x.endswith('avi'), videos)

    # Load the CNN model trained on images to recognize objects.
    with tf.device('/gpu:0'):
        with tf.Session() as sess:
            vgg = vgg16.Vgg16()

    for idx, video in enumerate(videos):
        print idx, video

        if os.path.exists(os.path.join(video_save_path, video)):
            print "Already processed ... "
            continue

        video_fullpath = os.path.join(video_path, video)
        try:
            cap = cv2.VideoCapture(video_fullpath)
        except:
            print "cv2 not working"
            pass

        frame_count = 0
        frame_list = []

        while True:
            ret, frame = cap.read()
            if ret is False:
                break

            frame_list.append(frame)
            frame_count += 1

        frame_list = np.array(frame_list)
        if frame_count > 80:
            frame_indices = np.linspace(0,
                                        frame_count,
                                        num=num_frames,
                                        endpoint=False).astype(int)
            frame_list = frame_list[frame_indices]

        processed_frame_list = []
        for x in frame_list:
            processed_frame_list.append(preprocess_frame(x))

        # Generate the frame list of equally spaced 80 frames.
        cropped_frame_list = np.array(processed_frame_list)
        print "Cropped Frame List: ", cropped_frame_list.shape
        images = tf.placeholder("float",
                                [cropped_frame_list.shape[0], 224, 224, 3])
        with tf.name_scope("content_vgg"):
            vgg.build(images)
        feats = sess.run(vgg.fc6, feed_dict=feed_dict)
        print "Feature Length: ", len(feats)
        save_full_path = os.path.join(video_save_path, video + '.npy')
        np.save(save_full_path, feats)
Exemple #9
0
def  predict(models_path,image_dir,labels_filename,labels_nums, data_format):
    [batch_size, resize_height, resize_width, depths] = data_format

    labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(dtype=tf.float32, shape=[None, resize_height, resize_width, depths], name='input')

    #其他模型预测请修改这里
    # with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
    #    out, end_points = inception_v3.inception_v3(inputs=input_images, num_classes=labels_nums, dropout_keep_prob=1.0, is_training=False)
    vgg = vgg16.Vgg16(vgg16_npy_path='./vgg16.npy',input=input_images)
    out = vgg.out
    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out,name='pre')
    class_id = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)
    images_list=glob.glob(os.path.join(image_dir,'*.jpg'))
    for image_path in images_list:
        im=read_image(image_path,resize_height,resize_width,normalization=True)
        im=im[np.newaxis,:]
        #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pre_score,pre_label = sess.run([score,class_id], feed_dict={input_images:im})
        max_score=pre_score[0,pre_label]
        img = cv2.imread(image_path)
        plt.imshow(img)
        plt.title(os.path.basename(image_path))
        print("{} is: pre labels:{},name:{} score: {}".format(image_path,pre_label,labels[pre_label], max_score))
        plt.show()  #自己加的
    sess.close()
Exemple #10
0
def get_feats(image_dict):
    vgg16_feats = np.zeros((len(image_dict), 4096))
    with tf.Session() as sess:
        images = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3])
        vgg = vgg16.Vgg16()
        vgg.build(images)
        for i in range(len(image_dict)):
            print(i)
            img_list = load_image(image_dict[i])
            batch = img_list.reshape((1, 224, 224, 3))
            print(batch.shape)
            feature = sess.run(vgg.fc7, feed_dict={images: batch})  #提取fc7层的特征
            print(feature.shape)
            feature = np.reshape(feature, [4096])
            feature = feature.reshape(-1)
            print(feature.shape)
            feature /= norm(feature)  # 特征归一化
            #           print(feature)
            vgg16_feats[i, :] = feature  #每张图片的特征向量为1行
#           print(vgg16_feats.shape)


#            print(len(vgg16_feats))
#            print(len(vgg16_feats[0]))
#    vgg16_feats = np.save('X:/github/vgg-tensorflow/vgg16_feats', vgg16_feats)
    return vgg16_feats
def get_activations(layer_name, batch):
	#uncomment for gpu
	# with tf.Session(config=tf.ConfigProto(gpu_options=(tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess:

	with tf.device('/cpu:0'):
		with tf.Session() as sess:
			images = tf.placeholder("float", [len(batch), 224, 224, 3])
			feed_dict = {images: batch}

			vgg = vgg16.Vgg16()
			with tf.name_scope("content_vgg"):
				vgg.build(images)
			
			map_layer_name = {}
			map_layer_name['conv1_1'] = vgg.conv1_1
			map_layer_name['conv1_2'] = vgg.conv1_2
			map_layer_name['conv2_1'] = vgg.conv2_1
			map_layer_name['conv2_2'] = vgg.conv2_2
			map_layer_name['conv3_1'] = vgg.conv3_1
			map_layer_name['conv3_2'] = vgg.conv3_2
			map_layer_name['conv3_3'] = vgg.conv3_3
			map_layer_name['conv4_1'] = vgg.conv4_1
			map_layer_name['conv4_2'] = vgg.conv4_2
			map_layer_name['conv4_3'] = vgg.conv4_3
			map_layer_name['conv5_1'] = vgg.conv5_1
			map_layer_name['conv5_2'] = vgg.conv5_2
			map_layer_name['conv5_3'] = vgg.conv5_3
			map_layer_name['fc6'] = vgg.fc6
			map_layer_name['fc7'] = vgg.fc7
			map_layer_name['fc8'] = vgg.fc8

			activations = sess.run(map_layer_name[layer_name], feed_dict=feed_dict)
			return activations
Exemple #12
0
def main(_):
    input_ = tf.placeholder(tf.float32, [None, 224, 224, 3])
    vgg = vgg16.Vgg16()
    vgg.build(input_)
    with tf.Session() as sess:
        img = load_image(FLAGS.image_path)
        img = img.reshape((1, 224, 224, 3))

        feed_dict = {input_: img}
        codes = sess.run(vgg.relu6, feed_dict=feed_dict)

    with tf.Session() as sess:
        feature_columns = [
            tf.feature_column.numeric_column('code', shape=4096)
        ]
        classifier = tf.estimator.DNNClassifier(
            feature_columns=feature_columns,
            hidden_units=[512],
            n_classes=5,
            model_dir='./estimator_model_dir')
        # train_input_fun = create_input_fun(FLAGS.train_tfrecords, repeat_count=2)
        # classifier.train(input_fn=train_input_fun)
        # test_input_fun = create_input_fun(FLAGS.test_tfrecords, repeat_count=1, perform_shuffle=False)
        validate_input_fun = create_validate_input_fun(codes)
        # evaluate_result = classifier.evaluate(input_fn=test_input_fun)
        # print("Evaluation results:")
        # for key in evaluate_result:
        #     print("   {}, was: {}".format(key, evaluate_result[key]))

        predict_results = classifier.predict(input_fn=validate_input_fun)
        print("Predictions:")
        print(
            list(
                map(lambda x: INDEX_LABEL_MAP[x["class_ids"][0]],
                    predict_results)))
Exemple #13
0
 def __init__(self):
     self.vgg_t = vgg16.Vgg16()
     
    
     self.input_holder_t = tf.placeholder(tf.float32, [1, img_size, img_size, 3])
    
     self.label_holder = tf.placeholder(tf.float32, [label_size*label_size, 2])
Exemple #14
0
def compute_gram_matrices(
        images,
        vgg_layers=['conv1_2', 'conv2_2', 'conv3_2', 'conv4_2', 'conv5_2']):
    """Computes the gram matrix representation of a batch of images"""
    vgg_net = vgg16.Vgg16(opts.vgg16_path)
    vgg_acts = vgg_net.get_vgg_activations(images, vgg_layers)
    grams = [gram_matrix(layer) for layer in vgg_acts]
    return grams
Exemple #15
0
 def G_Net(self, x, keep_prob):
     vgg = vgg16.Vgg16()
     print('making g-network')
     with tf.variable_scope('G_Net'):
         #x = norm_image(x)
         A_input, S_input = vgg.build(x, self.batch_size, keep_prob)
     print('finishing g-network')
     return A_input, S_input
Exemple #16
0
 def perc_loss_cal(self, input_tensor):
     """
     得到输入图像在预训练的VGG16模型中的中间层,用于计算感知损失
     :param input_tensor: 输入人像
     :return: 输入人像的conv4_1参数
     """
     vgg = vgg16.Vgg16("./preTrainedModel/vgg16.npy")
     vgg.build(input_tensor)
     return vgg.conv4_1
    def __init__(self):
        self.vgg = vgg16.Vgg16()

        self.input_holder = tf.placeholder(tf.float32, [1, img_size, img_size, 3])
        self.label_holder = tf.placeholder(tf.float32, [label_size*label_size, 2])

        self.sobel_fx, self.sobel_fy = self.sobel_filter()

        self.contour_th = 1.5
        self.contour_weight = 0.0001
Exemple #18
0
    def __init__(self):
        self.vgg = vgg16.Vgg16()

        self.input_holder = tf.placeholder(tf.float32,
                                           [5, img_size, img_size, 3])
        self.label_holder = tf.placeholder(tf.float32,
                                           [5, label_size, label_size, 2])

        self.contour_th = 1.5
        self.contour_weight = 0.0001
Exemple #19
0
def compute_vgg_loss(images, layer_names, batch_size, ch, vgg_weight):
    # images = [ba, B , ab, A]
    """ Preprocess images """
    processed = [vgg_preprocess(i) for i in images]
    #ba = vgg_preprocess( ba )
    #ab = vgg_preprocess( ab )
    #B = vgg_preprocess( B )
    #A = vgg_preprocess( A )

    img_shape = processed[-1].get_shape()
    h = img_shape[1]
    w = img_shape[2]

    layers_ = list()

    with tf.name_scope("VGG16"):
        # Create first instance of the VGG16-model
        vgg1 = vgg16.Vgg16(vgg_weight, h, w, ch)

        for enum, i in enumerate(processed):
            with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                with tf.name_scope("p" + str(enum)):
                    vgg1.build(i)
                    layers_.append(vgg1.get_layer_tensors(layer_names, enum))

        # vgg1.build( ba )
        # layers_ba = vgg1.get_layer_tensors( layer_names )
        # vgg1.build( A )
        # layers_A = vgg1.get_layer_tensors( layer_names )
        # vgg1.build( ab )
        # layers_ab = vgg1.get_layer_tensors( layer_names )

        vgg1.data_dict = None

        # Create second instance of the VGG16-model
        # vgg1 = vgg16.Vgg16( vgg_weight, h, w, ch )
        # vgg1.build( ba )
        # layers_ba = vgg1.get_layer_tensors( layer_names )

        # compute perceptual loss
        with tf.variable_scope('loss_a', reuse=tf.AUTO_REUSE):
            loss_a = tf.zeros(batch_size, tf.float32)
            for f, f_ in zip(layers_[0], layers_[1]):
                loss_a += tf.reduce_mean(
                    tf.subtract(instance_norm(f), instance_norm(f_))**2,
                    [1, 2, 3])

        with tf.variable_scope('loss_b', reuse=tf.AUTO_REUSE):
            loss_b = tf.zeros(batch_size, tf.float32)
            for f, f_ in zip(layers_[2], layers_[3]):
                loss_b += tf.reduce_mean(
                    tf.subtract(instance_norm(f), instance_norm(f_))**2,
                    [1, 2, 3])

    return tf.reduce_mean(loss_a), tf.reduce_mean(loss_b)
    def __init__(self):
        self.vgg = vgg16.Vgg16()

        self.input_holder = tf.placeholder(tf.float32,
                                           [1, img_size, img_size, 3])
        self.label_holder = tf.placeholder(tf.float32,
                                           [label_size * label_size, 2])
        self.label_noise = tf.placeholder(tf.float32,
                                          [1, label_size, label_size, 1])

        self.training = tf.placeholder(tf.bool, shape=[])
def load_model(name, path):
    if (name == "vgg19"):
        model = vgg19.Vgg19(get_data_mean(),
                            path,
                            trainable=False,
                            skippable=False)
    if (name == "vgg16"):
        model = vgg16.Vgg16(get_data_mean(),
                            path,
                            trainable=False,
                            skippable=False)
    return model
Exemple #22
0
def test():
    with tf.Graph().as_default() as g:
        x = tf.placeholder(
            tf.float32,
            [TEST_NUM, vgg16.IMAGE_SIZE, vgg16.IMAGE_SIZE, vgg16.NUM_CHANNELS])
        y_ = tf.placeholder(tf.float32, [None, 2])
        vgg = vgg16.Vgg16()
        y = vgg.forward(x, False, None)

        ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        img_batch, label_batch = tfrecords_generateds.get_tfrecord(
            TEST_NUM, isTrain=False)

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split(
                        '/')[-1].split('-')[-1]

                    coord = tf.train.Coordinator()
                    threads = tf.train.start_queue_runners(sess=sess,
                                                           coord=coord)

                    score = []
                    for i in range(10):
                        xs, ys = sess.run([img_batch, label_batch])
                        reshaped_xs = np.reshape(
                            xs, (TEST_NUM, vgg16.IMAGE_SIZE, vgg16.IMAGE_SIZE,
                                 vgg16.NUM_CHANNELS))
                        accuracy_score = sess.run(accuracy,
                                                  feed_dict={
                                                      x: reshaped_xs,
                                                      y_: ys
                                                  })
                        score.append(accuracy_score)

                    print("After %s training step(s), test accuracy = %g" %
                          (global_step, np.mean(score)))
                    # print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                    coord.request_stop()
                    coord.join(threads)
                else:
                    print('No checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS)
Exemple #23
0
def main(args):
    train_data = 'expression/fer2013-train'
    test_data = 'expression/fer2013-test'

    train_img, train_label = load_image(train_data)
    test_img, test_label = load_image(test_data)

    images = tf.placeholder(tf.float32, [None, 224, 224, 3])
    label = tf.placeholder(tf.float32, [None, 7])
    if args.network_model == "vgg16":
        vgg = vgg16.Vgg16(args.fine_tuning)
    else:
        vgg = vgg19.Vgg19(args.fine_tuning)
    predict = vgg.build(images)
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=predict))
    #if fine_tuning='all',we will use GradientDescent to optimize all variables
    #if fine_tuning!='all',we will only use Adam to optimize the fully connected layers
    if args.fine_tuning == "all":
        optimizer = tf.train.GradientDescentOptimizer(
            learning_rate=0.001).minimize(cross_entropy)
    else:
        optimizer = tf.train.AdamOptimizer(
            learning_rate=0.001).minimize(cross_entropy)
    correct = tf.equal(tf.argmax(tf.nn.softmax(predict), 1),
                       tf.argmax(label, 1))
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver(tf.all_variables())
        train = next_batch(train_img, train_label, 32)
        for i in range(args.train_step):
            x_batch, y_batch = train.next()
            loss, _, acc, = sess.run([cross_entropy, optimizer, accuracy],
                                     feed_dict={
                                         images: x_batch,
                                         label: y_batch
                                     })
            if i % 10 == 0:
                saver.save(sess, 'save_variables/vgg.module', global_step=i)
                print('number %d loss is %f' % (i, loss))
                print('number %d accuracy is %f' % (i, acc))
        test_accuracy = 0
        test = next_batch(test_img, test_label, 32)
        for j in range(100):
            x_batch, y_batch = test.next()
            acc = sess.run(accuracy,
                           feed_dict={
                               images: x_batch,
                               label: y_batch
                           })
            test_accuracy += acc
        print('test accuracy is %f' % (test_accuracy / 100))
Exemple #24
0
def run_train():
    fout = open('inf.txt', 'w+')

    test_config = ModelConfig()
    test_config.keep_prob = 1.0
    test_config.batch_size = 1

    Session_config = tf.ConfigProto(allow_soft_placement=True)
    Session_config.gpu_options.allow_growth = True

    with tf.Graph().as_default(), tf.Session(config=Session_config) as sess:
        with tf.device('/gpu:0'):
            #if True:
            initializer = tf.random_uniform_initializer(
                -test_config.init_scale, test_config.init_scale)

            train_model = vgg16.Vgg16(FLAGS.vgg16_file_path)
            train_model.build(initializer)

            data_test = dataset.DataSet(FLAGS.file_path_test,
                                        FLAGS.data_root_dir,
                                        TEST_SIZE,
                                        is_train_set=False)

            test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test')

            saver = tf.train.Saver(max_to_keep=100)
            last_epoch = load_model(sess, saver, FLAGS.saveModelPath,
                                    train_model)
            print('start: ', last_epoch + 1)

            test_accury_1, test_accury_5, test_loss = run_epoch(
                sess,
                test_config.keep_prob,
                fout,
                test_config.batch_size,
                train_model,
                data_test,
                tf.no_op(),
                2,
                test_writer,
                istraining=False)
            info = "Final: Test accury(top 1): %.4f Test accury(top 5): %.4f Loss %.4f" % (
                test_accury_1, test_accury_5, test_loss)
            print(info)
            fout.write(info + '\n')
            fout.flush()

            test_writer.close()

            print("Training step is compeleted!")
            fout.close()
def set_global_xnet(gpuids):
    #from wingdebugger import wingdbstub
    #wingdbstub.Ensure()
    #wingdbstub.debugger.StartDebug()
    global xnet
    gpuid = gpuids.get()
    print('#############################', gpuids)
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpuid)
    import vgg16
    xnet = vgg16.Vgg16(
        '/media/zhaoke/b0685ee4-63e3-4691-ae02-feceacff6996/models/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
    )
def get_feats():
    vgg16_feats = np.zeros((len(unique_images), 4096))
    with tf.Session() as sess:
        images = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3])
        vgg = vgg16.Vgg16()
        vgg.build(images)
        for i in range(len(unique_images)):
            img_list = utils.load_image(unique_images[i])
            batch = img_list.reshape((1, 224, 224, 3))
            feature = sess.run(vgg.fc7, feed_dict={images: batch})#提取fc7层的特征
            feature = np.reshape(feature, [4096])
	     feature /= norm(feature) # 特征归一化
            vgg16_feats[i, :] = feature #每张图片的特征向量为1行
Exemple #27
0
def test(batch):
    with tf.Session(
            config=tf.ConfigProto(gpu_options=(
                tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess:
        images = tf.placeholder("float", [None, 224, 224, 3])
        feed_dict = {images: batch}

        vgg = vgg16.Vgg16()
        with tf.name_scope("content_vgg"):
            vgg.build(images)

        prob = sess.run(vgg.prob, feed_dict=feed_dict)
        print(prob)
        utils.print_prob(prob[0], './synset.txt')
Exemple #28
0
def app():
    csvfile = open("./npy/submission.csv", "w")
    Writer = csv.writer(csvfile, delimiter=',', lineterminator='\r\n')
    Writer.writerow(['id', 'label'])

    with tf.Graph().as_default() as g:

        images = tf.placeholder(tf.float32, [1, 224, 224, 3])
        vgg = vgg16.Vgg16()
        y = vgg.forward(images, False, None)
        y = tf.nn.softmax(y)

        ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)

            ckpt = tf.train.get_checkpoint_state(backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]

                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(sess=sess, coord=coord)

                for num in range(1, 5):
                    img_ready = utils.load_image(path + str(num) +
                                                 '.jpg') / 255.0
                    img_ready = img_ready.reshape((1, 224, 224, 3))

                    probability = sess.run(y, feed_dict={images: img_ready})
                    # print(probability, probability[0][0]+probability[0][1])
                    top5 = np.argsort(probability[0])[-1:-3:-1]

                    row = [num]
                    row.append(probability[0][1])
                    print(num, probability[0][1])
                    Writer.writerow(row)
                csvfile.close()
                print("write finished")

                coord.request_stop()
                coord.join(threads)
            else:
                print('No checkpoint file found')
Exemple #29
0
def batch_vgg(training_tuple):
    batch = np.concatenate(tuple(training_tuple), 0)
    tf.reset_default_graph()
    with tf.Session() as sess:
        images = tf.placeholder("float", [training_size, 224, 224, 3])
        feed_dict = {images: batch}

        vgg = vgg16.Vgg16()
        vgg.build(images)

        prob = sess.run(vgg.prob, feed_dict=feed_dict)

    print 'assign prob'
    for x, y in zip(training_pic_name, prob):
        pic_dict[x] = y
Exemple #30
0
def main():
    # load an img file, preprocess and reshape it
    img_original, img_reshaped = load_img(
        './Black_Footed_Albatross_0001_796111.jpg')

    # open a session
    sess = open_sess("0")

    # build a model
    images = tf.placeholder("float", [1, config["img_W"], config["img_H"], 3])

    vgg = vgg16.Vgg16()
    vgg.build(images)

    feed_dict = {images: img_reshaped}
    prob = sess.run(vgg.prob, feed_dict=feed_dict)
    #print(prob)

    # get the class map value
    target_class = [np.argmax(prob)]  # the class that we're interested in
    output = vgg.fc8  # the output(score) layer before the softmax
    last_conv = vgg.conv5_3  # the last feature maps

    label = tf.placeholder(tf.int64, [None], name='label')
    classmap = get_classmap(label, output, last_conv, config["img_W"],
                            config["img_H"])

    gradcam = sess.run(classmap,
                       feed_dict={
                           images: img_reshaped,
                           label: target_class
                       })

    # draw the class map
    plt.imshow(img_original.resize([config["img_W"], config["img_H"]]),
               cmap='gray')
    #plt.imshow(gradcam[0], cmap=plt.cm.seismic, alpha=0.5, interpolation='nearest')    # normalized
    plt.imshow(gradcam[0],
               cmap=plt.cm.seismic,
               alpha=0.5,
               interpolation='nearest',
               vmin=-0.4,
               vmax=0.4)
    plt.savefig('test.png', dpi=256)
    plt.close()