def test(path):
    x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3], name='input')
    keep_prob = tf.placeholder(tf.float32)
    output = VGG16(x, keep_prob, n_cls)
    score = tf.nn.softmax(output)
    f_cls = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, '/home/chengrui/PycharmProjects/deeplearning/greenhouse/model/model.ckpt-130')
    a = 0
    b = 0
    for i in os.listdir(path):
        imgpath = os.path.join(path, i)
        im = cv2.imread(imgpath)
        im = cv2.resize(im, (224, 224))  # * (1. / 255)
        im = np.expand_dims(im, axis=0)
        # pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pred, _score = sess.run([f_cls, score], feed_dict={x: im, keep_prob: 1.0})
        prob = round(np.max(_score), 4)
        # print "{} flowers class is: {}".format(i, pred)
        print("{} flowers class is: {}, score: {}".format(i, int(pred), prob))

        if int(pred)==1:
            a+=1

        b+=1
        print(a)
        print(b)

    sess.close()
Beispiel #2
0
def train():
    x = tf.placeholder(tf.float32, shape=[None, 224, 224, 3], name='input')
    y = tf.placeholder(tf.int64, shape=[None, n_classes], name='label')
    keep_prob = tf.placeholder(tf.float32)
    output = VGG16(x, keep_prob, n_classes)

    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(logits=output, labels=y))

    train_step = tf.train.GradientDescentOptimizer(
        learning_rate=lr).minimize(loss)

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

    images, labels = read_and_decode('train.tfrecords')
    img_batch, label_batch = tf.train.shuffle_batch([images, labels],
                                                    batch_size=batch_size,
                                                    capacity=512,
                                                    min_after_dequeue=200)
    label_batch = tf.one_hot(label_batch, n_classes, 1, 0)

    init = tf.global_variables_initializer()

    saver = tf.train.Saver()

    plot_loss = []
    fig = plt.figure()
    with tf.Session() as sess:
        sess.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps):
            batch_x, batch_y = sess.run([img_batch, label_batch])
            _, loss_val = sess.run([train_step, loss],
                                   feed_dict={
                                       x: batch_x,
                                       y: batch_y,
                                       keep_prob: 1.0
                                   })
            if i % 100 == 0:
                train_acc = sess.run(accuracy,
                                     feed_dict={
                                         x: batch_x,
                                         y: batch_y,
                                         keep_prob: 1.0
                                     })
                print("%s: Step [%d] Loss: %f, training accuracy: %g" %
                      (datetime.now(), i, loss_val, train_acc))

            plot_loss.append(loss_val)

            if (i + 1) == max_steps:
                saver.save(sess, './model/model.ckpt', global_step=i)

        coord.request_stop()
        coord.join(threads)

        plt.plot(plot_loss, c='r')
        plt.show()
Beispiel #3
0
def train():
    x = tf.placeholder(dtype=tf.float32,
                       shape=[None, 224, 224, 3],
                       name='input')
    y = tf.placeholder(dtype=tf.float32, shape=[None, n_cls], name='label')
    keep_prob = tf.placeholder(tf.float32)
    output = VGG16(x, keep_prob, n_cls)
    #probs = tf.nn.softmax(output)

    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
    #train_step = tf.train.AdamOptimizer(learning_rate=0.1).minimize(loss)
    train_step = tf.train.GradientDescentOptimizer(
        learning_rate=lr).minimize(loss)

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

    images, labels = read_and_decode(
        '/home/chengrui/PycharmProjects/deeplearning/greenhouse/train.tfrecords'
    )
    img_batch, label_batch = tf.train.shuffle_batch([images, labels],
                                                    batch_size=batch_size,
                                                    capacity=392,
                                                    min_after_dequeue=200)
    label_batch = tf.one_hot(label_batch, n_cls, 1, 0)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps):
            batch_x, batch_y = sess.run([img_batch, label_batch])
            #            print batch_x, batch_x.shape
            #            print batch_y
            #            pdb.set_trace()
            _, loss_val = sess.run([train_step, loss],
                                   feed_dict={
                                       x: batch_x,
                                       y: batch_y,
                                       keep_prob: 0.8
                                   })
            if i % 10 == 0:
                train_arr = accuracy.eval(feed_dict={
                    x: batch_x,
                    y: batch_y,
                    keep_prob: 1.0
                })
                print("%s: Step [%d]  Loss : %f, training accuracy :  %g" %
                      (datetime.now(), i, loss_val, train_arr))
            if (i + 1) == max_steps:
                #checkpoint_path = os.path.join(FLAGS.train_dir, './model/model.ckpt')
                saver.save(
                    sess,
                    '/home/chengrui/PycharmProjects/deeplearning/greenhouse/model/model.ckpt',
                    global_step=i)
        coord.request_stop()
        coord.join(threads)
Beispiel #4
0
def try_a_try(path):
    x = tf.placeholder(tf.float32, shape=[None, 224, 224, 3], name='input')
    keep_prob = tf.placeholder(tf.float32)
    output = VGG16(x, keep_prob, 17)
    score = tf.nn.softmax(output)
    f_cls = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()

    saver.restore(sess, './model/model.ckpt-4999')
    for i in os.listdir(path):
        imgpath = os.path.join(path, i)
        im = cv2.imread(imgpath)
        im = cv2.resize(im, (224, 224))

        im = np.expand_dims(im, axis=0)

        pred, _score = sess.run([f_cls, score],
                                feed_dict={
                                    x: im,
                                    keep_prob: 1.0
                                })
        prob = round(np.max(_score))
        print("{} flowers class is {}, score: {} ".format(i, int(pred), prob))

    sess.close()
def VGG16Net_Training():
    if torch.cuda.is_available():
        model = VGG16.VGG16().cuda()
    else:
        model = VGG16.VGG16()

    trainset = VGG16_InputData.LoadDataset(data_path=data_path)
    # print(len(trainset))
    train_loader = DataLoader(dataset=trainset, batch_size=20, shuffle=True)

    lr = 1e-3
    loss_func = torch.nn.CrossEntropyLoss().cuda()
    # optimizer = torch.optim.SGD(list(model.parameters())[:], lr=lr, momentum=0.9)
    optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=0.9)

    for epoch in range(10000):
        print('epoch {}'.format(epoch + 1))
        # training-----------------------------
        train_loss = 0.
        train_acc = 0.
        start_time = time.time()
        for trainData, trainLabel in train_loader:
            # trainData, trainLabel = Variable(trainData.cuda()), Variable(trainLabel.cuda())
            trainData, trainLabel = trainData.cuda(), trainLabel.cuda()
            out = model(trainData)
            loss = loss_func(out, trainLabel)
            train_loss += loss.item()
            pred = torch.max(out, 1)[1]
            train_correct = (pred == trainLabel).sum()
            train_acc += train_correct.item()
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        #  if epoch % 100 == 0:
        print("speed time : {:.3f}".format((time.time() - start_time)))
        print('Train Loss: {:.6f}, Acc: {:.6f}'.format(
            train_loss / (len(trainset)), train_acc / (len(trainset))))

        if (epoch + 1) % 10 == 0:
            sodir = './model/_iter_{}.pth'.format(epoch + 1)
            print('[5] Model save {}'.format(sodir))
            torch.save(model.state_dict(), sodir)

        # adjust
        if (epoch + 1) % 50 == 0:
            lr = lr / 10
            optimizer = torch.optim.SGD(model.parameters(), lr=lr)
Beispiel #6
0
def FCN(x, is_training, dropout_rate = 0.25):
    x -= VGG_MEAN

    with tf.contrib.slim.arg_scope(vgg.vgg_arg_scope()):
        feature_maps = vgg.vgg_16(x, num_classes=1000, is_training=is_training, dropout_keep_prob=0.5)
    
    x = feature_maps[4] # pool5 = 7x7x512

    x = tf.layers.conv2d(inputs = x, filters = 4096, kernel_size = [1, 1], strides = 1, padding = 'SAME', kernel_initializer = init_fn, name = 'conv_6')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'bn_6')
    x = tf.nn.relu(x, name = 'relu_6')
    x = tf.layers.dropout(x, dropout_rate, training = is_training, name = 'dropout_6')

    x = tf.layers.conv2d(inputs = x, filters = 4096, kernel_size = [1, 1], strides = 1, padding = 'SAME', kernel_initializer = init_fn, name = 'conv_7')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'bn_7')
    x = tf.nn.relu(x, name = 'relu_7')
    x = tf.layers.dropout(x, dropout_rate, training = is_training, name = 'dropout_7')
    # print(x)

    shape = feature_maps[3].get_shape().as_list()
    x = tf.layers.conv2d_transpose(inputs = x, filters = shape[-1], kernel_size = [3, 3], strides = 2, padding = 'SAME', kernel_initializer = init_fn, name = 'up_conv2d_1')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'up_bn_1')
    x = tf.nn.relu(x, name = 'up_relu_1')
    x += feature_maps[3]
    # print(x)

    shape = feature_maps[2].get_shape().as_list()
    x = tf.layers.conv2d_transpose(inputs = x, filters = shape[-1], kernel_size = [3, 3], strides = 2, padding = 'SAME', kernel_initializer = init_fn, name = 'up_conv2d_2')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'up_bn_2')
    x = tf.nn.relu(x, name = 'up_relu_2')
    x += feature_maps[2]
    # print(x)

    shape = feature_maps[1].get_shape().as_list()
    x = tf.layers.conv2d_transpose(inputs = x, filters = shape[-1], kernel_size = [16, 16], strides = 8, padding = 'SAME', kernel_initializer = init_fn, name = 'up_conv2d_3')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'up_bn_3')
    x = tf.nn.relu(x, name = 'up_relu_3')
    # print(x)
    
    x = tf.layers.conv2d(inputs = x, filters = CLASSES, kernel_size = [1, 1], strides = 1, padding = 'SAME', kernel_initializer = init_fn, name = 'conv2d')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'bn')

    # predictions = tf.nn.softmax(x, axis = -1, name = 'predictions')
    predictions = x

    return predictions
Beispiel #7
0
def FCN_UNet(input_var, is_training):
    x = input_var - VGG_MEAN

    i_x = tf.layers.conv2d(inputs = x, filters = 64, kernel_size = [3, 3], strides = 1, padding = 'SAME', kernel_initializer = init_fn, name = 'i_conv')
    i_x = tf.layers.batch_normalization(inputs = i_x, training = is_training, name = 'i_bn')
    i_x = tf.nn.relu(i_x, name = 'i_relu')
    
    with tf.contrib.slim.arg_scope(vgg.vgg_arg_scope()):
        feature_maps = vgg.vgg_16(x, num_classes=1000, is_training=is_training, dropout_keep_prob=0.5)
    
    # resolution 224x224 -> feature_maps[4], feature_maps[3 - i]
    # resolution 112x112 -> feature_maps[3], feature_maps[2 - i]
    x = feature_maps[3]
    for i in range(len(feature_maps) - 2):
        prior_feature_map = feature_maps[2 - i]
        shape = prior_feature_map.get_shape().as_list()

        x = tf.layers.conv2d_transpose(inputs = x, filters = shape[-1], kernel_size = [3, 3], strides = 2, padding = 'SAME', kernel_initializer = init_fn, name = 'up_conv2d_{}'.format(i))
        x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'up_bn_{}'.format(i))
        x = tf.nn.relu(x, name = 'up_relu_{}'.format(i))
        
        #print(x, prior_feature_map); input()
        x = tf.concat((x, prior_feature_map), axis = -1)
        
        x = tf.layers.conv2d(inputs = x, filters = shape[-1], kernel_size = [3, 3], strides = 1, padding = 'SAME', kernel_initializer = init_fn, name = 'conv_{}'.format(i))
        x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'bn_{}'.format(i))
        x = tf.nn.relu(x, name = 'relu_{}'.format(i))

    x = tf.layers.conv2d_transpose(inputs = x, filters = shape[-1], kernel_size = [3, 3], strides = 2, padding = 'SAME', kernel_initializer = init_fn, name = 'up_conv2d')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'up_bn')
    x = tf.nn.relu(x, name = 'up_relu')

    x = tf.concat((i_x, x), axis = -1)

    x = tf.layers.conv2d(inputs = x, filters = CLASSES, kernel_size = [1, 1], strides = 1, padding = 'SAME', kernel_initializer = init_fn, name = 'conv2d')
    x = tf.layers.batch_normalization(inputs = x, training = is_training, name = 'bn')

    logits = x
    predictions = tf.nn.softmax(x, axis = -1, name = 'predictions')

    return logits, predictions
def train(record_file,train_param,class_nums,data_format):
    [base_lr,max_steps,snapshot,snapshot_prefix,display]=train_param
    [batch_size,resize_height,resize_width,channels]=data_format

    # 定义x为图片数据
    x = tf.placeholder(dtype=tf.float32, shape=[None, resize_height,resize_width,channels], name='input')
    # 定义y为labels数据
    y = tf.placeholder(dtype=tf.float32, shape=[None, class_nums], name='label')
    keep_prob = tf.placeholder(tf.float32)
    output = VGG16(x, keep_prob, class_nums)
    #probs = tf.nn.softmax(output)

    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
    #train_step = tf.train.AdamOptimizer(learning_rate=0.1).minimize(loss)
    train_step = tf.train.GradientDescentOptimizer(learning_rate=base_lr).minimize(loss)

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

    # 从record中读取图片和labels数据
    # images, labels = read_and_decode('./train.tfrecords')
    images,labels = read_records(record_file,resize_height, resize_width) # 读取函数

    img_batch, label_batch = tf.train.shuffle_batch([images, labels],
                                                    batch_size=batch_size,
                                                    capacity=392,
                                                    min_after_dequeue=200)
    label_batch = tf.one_hot(label_batch, class_nums, 1, 0)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps+1):
            batch_x, batch_y = sess.run([img_batch, label_batch])
#            print batch_x, batch_x.shape
#            print batch_y
#            pdb.set_trace()
            _, loss_val = sess.run([train_step, loss], feed_dict={x:batch_x, y:batch_y, keep_prob:0.8})
            if i%display == 0:
                train_arr = accuracy.eval(feed_dict={x:batch_x, y: batch_y, keep_prob: 1.0})
                print "%s: Step [%d]  Loss : %f, training accuracy :  %g" % (datetime.now(), i, loss_val, train_arr)
            # 每迭代snapshot次或者最后一次保存模型
            if (i %snapshot == 0 and i >0)or i == max_steps:
                print('save:{}-{}'.format(snapshot_prefix,i))
                #checkpoint_path = os.path.join(FLAGS.train_dir, './model/model.ckpt')
                saver.save(sess, snapshot_prefix, global_step=i)
        coord.request_stop()
        coord.join(threads)
def main():
    max_epoch = int(sys.argv[2])
    batch_size = int(sys.argv[3])

    with tf.Session() as sess:
        image = tf.placeholder(tf.float32, [None, 224, 224, 3])
        label = tf.placeholder(tf.float32, [None, 1000])

        train_data, train_mean = do.load_train_data(sys.argv[1])
        train_size = len(train_data)

        vgg_model = vgg.VGG16(None, train_mean, True)
        with tf.name_scope('vgg_content'):
            vgg_model.build(image)

        loss = tf.nn.softmax_cross_entropy_with_logits(logits=vgg_model.fc8,
                                                       labels=label)
        loss_mean = tf.reduce_mean(loss)
        optimizer = tf.train.AdamOptimizer(
            learning_rate=0.1).minimize(loss_mean)

        correct_prediction = tf.equal(tf.argmax(vgg_model.fc8, 1),
                                      tf.argmax(label, 1))
        accuracy_mean = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        writer = tf.summary.FileWriter('./log/', sess.graph)

        sess.run(tf.global_variables_initializer())

        for epoch_idx in range(max_epoch):
            for batch_idx in range(train_size // batch_size):
                batch_image, batch_label = do.get_batch_data(
                    sess, train_data, batch_size)
                feed_dict = {image: batch_image, label: batch_label}

                _, loss_mean_value = sess.run([optimizer, loss_mean],
                                              feed_dict=feed_dict)
                print_batch_info(epoch_idx, batch_idx, loss_mean_value)

            batch_image, batch_label = do.get_batch_data(
                sess, train_data, batch_size)
            feed_dict = {image: batch_image, label: batch_label}

            accuracy_mean_value = sess.run(accuracy_mean, feed_dict=feed_dict)
            print_epoch_info(epoch_idx, accuracy_mean_value)

        do.save_npy(sess, vgg_model.var_dict, vgg_model.npy_mean, sys.argv[4],
                    sys.argv[5])
Beispiel #10
0
def train():
    x = tf.placeholder(tf.float32, [batch_size, imge_size, image_size, 3],
                       name='x_input')
    y_ = tf.placeholder(tf.float32, [None, num_class], name='y_output')

    y = VGG16(X=x, NUM_CLASSES=num_class)
    global_step = tf.Variable(0, trainable=False)

    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y,
                                                            labels=y_)
    cross_entropy_mean = tf.reduce_mean(cross_entropy)

    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        cross_entropy_mean)

    correct_pred = tf.equal(tf.argmax(fc8, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
def main():
    with tf.Session() as sess:
        image = tf.placeholder(tf.float32, [1, 224, 224, 3])

        npy_file, npy_mean = do.load_npy(sys.argv[1], sys.argv[2])

        vgg_model = vgg.VGG16(npy_file, npy_mean, False)
        with tf.name_scope('vgg_content'):
            vgg_model.build(image)

        sess.run(tf.global_variables_initializer())

        img = do.load_image(sys.argv[4])
        feed_dict = {image: img}

        prob = sess.run(vgg_model.prob, feed_dict=feed_dict)
        print_result(sys.argv[3], prob[0], 5)
def test(path):
    x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3], name='input')
    keep_prob = tf.placeholder(tf.float32)
    output = VGG16(x, keep_prob, n_cls)
    score = tf.nn.softmax(output)
    f_cls = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, '/home/chengrui/PycharmProjects/deeplearning/greenhouse/model/model.ckpt-99')
    #加入字典
    label_name_dict={
        0:"大棚",
        1: "机场",
        2: "大桥",
        3: "商业区",
        4: "沙漠",
        5: "农田",
        6: "足球场",
        7: "森林",
        8: "工业区",
        9: "草地",
        10: "山地",
        11: "公园",
        12: "停车场",
        13: "码头",
        14: "居民区",
        15: "河流",
        16: "花",
    }
    for i in os.listdir(path):
        imgpath = os.path.join(path, i)
        im = cv2.imread(imgpath)
        im = cv2.resize(im, (224, 224))  # * (1. / 255)
        im = np.expand_dims(im, axis=0)
        # pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pred, _score = sess.run([f_cls, score], feed_dict={x: im, keep_prob: 1.0})
        prob = round(np.max(_score), 4)
        # print "{} flowers class is: {}".format(i, pred)
        pred_label=label_name_dict[int(pred)]
        print("图像名称{} ;该图像所属于类别: {};该类别所属概率: {}%".format(i, pred_label, prob*100))
    sess.close()
    def __init__(self):
        self.batch_size = 1
        self.test_num = np.zeros([1, 96, 96, 3], dtype=np.float32)
        self.image_holder = tf.placeholder(tf.float32,
                                           [self.batch_size, 96, 96, 3])
        self.label_holder = tf.placeholder(tf.int32, [self.batch_size])

        self.x = tf.placeholder(dtype=tf.float32,
                                shape=[1, 224, 224, 3],
                                name='input')
        self.keep_prob = tf.placeholder(tf.float32)
        self.output = VGG16(self.x, self.keep_prob, 10)
        self.score = tf.nn.softmax(self.output)
        self.f_cls = tf.argmax(self.score, 1)

        # self.sess = tf.InteractiveSession()
        self.sess = tf.Session()
        self.saver = tf.train.Saver()
        self.sess.run(tf.global_variables_initializer())
        self.saver.restore(self.sess, './model/model.ckpt-9999')
Beispiel #14
0
def main(_):
    tf.logging.set_verbosity(tf.logging.INFO)
    image_lists = dog_cat.create_image_lists(FLAGS.image_dir,
                                             FLAGS.testing_percentage,
                                             FLAGS.validation_percentage)
    class_count = len(image_lists.keys())
    if class_count == 0:
        tf.logging.error('No valid folders of images found at ' +
                         FLAGS.image_dir)
        return -1
    if class_count == 1:
        tf.logging.error('Only one valid folder of images found at ' +
                         FLAGS.image_dir +
                         ' - multiple classes are needed for classification.')
        return -1
    with tf.Session() as sess:
        (test_cached_tensor, test_ground_truth,
         _) = get_random_cached_bottlenecks(sess, image_lists,
                                            FLAGS.test_batch_size, 'testing',
                                            FLAGS.cache_dir, FLAGS.image_dir)
        logits = VGG16.vgg16_net(tf.convert_to_tensor(test_cached_tensor),
                                 class_count)
        correct = num_correct_prediction(logits, test_ground_truth)
        saver = tf.train.Saver(tf.global_variables())
        print("Reading checkpoints...")
        ckpt = tf.train.get_checkpoint_state('model')
        if ckpt and ckpt.model_checkpoint_path:
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
            saver.restore(sess, ckpt.model_checkpoint_path)
            print('Loading success, global_step is %s' % global_step)
        else:
            print('No checkpoint file found')
            return
        print('\nEvaluating......')
        batch_correct = sess.run(correct)
        print('Total testing samples: %d' % len(test_ground_truth))
        print('Total correct predictions: %d' % batch_correct)
        print('Average accuracy: %.2f%%' %
              (100 * batch_correct / len(test_ground_truth)))
Beispiel #15
0
    print('# prepare data')

    #path define
    model_dir = './model/'
    model_name = 'vgg16_{}.ckpt'
    model_path = model_dir + model_name

    #model build
    input_var = tf.placeholder(
        tf.float32,
        shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNEL],
        name='input')
    label_var = tf.placeholder(tf.float32, shape=[None, CLASSES])
    training_var = tf.placeholder(tf.bool)

    vgg = VGG16(input_var, training_var)
    print('# model build')

    learning_rate_var = tf.placeholder(tf.float32, name='learning_rate')

    #loss
    loss_op = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=label_var, logits=vgg))

    #optimizer
    extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(extra_update_ops):
        train_op = tf.train.AdamOptimizer(learning_rate=learning_rate_var,
                                          epsilon=1e-4).minimize(loss_op)

    #accuracy
Beispiel #16
0
def train_test():

    args = define_parse()

    # make labels and paths
    data_path = []
    labels = []
    label = -1
    for dir_path, dir_name, file_name in os.walk(directory):
        for file_name in file_name:
            data_path.append(os.path.join(dir_path, file_name))
            labels.append(label)
        label += 1

    # transform labels into one-hot-vector
    labels_onehot = to_categorical(labels)
    print(labels_onehot.shape)

    #何クラス分類なのか。今回は101クラス分類なので101が入る
    num_classes = label

    # split data to training and test data
    X_train, X_test, y_train, y_test = train_test_split(data_path,
                                                        labels_onehot,
                                                        train_size=0.8)

    # for making validation data
    X_train, X_val, y_train, y_val = train_test_split(X_train,
                                                      y_train,
                                                      train_size=0.8)

    # make Generator for fit_generator
    train_batch_generator = Generator.BatchGenerator(X_train, y_train,
                                                     batch_size, height, width)
    test_batch_generator = Generator.BatchGenerator(X_val, y_val, batch_size,
                                                    height, width)

    if args.mode == 'train':
        VGG = VGG16.vgg16(height, width, ch, num_classes)

        model = VGG.build_model()

        # training
        fit_history = model.fit_generator(
            train_batch_generator,
            epochs=epoch,
            verbose=1,
            steps_per_epoch=train_batch_generator.batches_per_epoch,
            validation_data=test_batch_generator,
            validation_steps=test_batch_generator.batches_per_epoch,
            shuffle=True)

        model.save(save_model_name)

        # evaluate
        '''
		score = model.evaluate_generator(test_batch_generator,
										step=train_batch_generator.batches_per_epoch,
										verbose=1)
		'''

    elif args.mode == 'test':
        model = load_model(load_model_name)

        # get class name for predicting
        class_name = []
        with open(class_path, "r") as file:
            for i in file:
                class_name.append(i.replace('\n', ''))
        class_name = np.asarray(class_name)

        # prediction
        img = load_img(X_test[0], target_size=(height, width))
        img_array = img_to_array(img) / 255  # normalization
        img_array = np.expand_dims(img_array,
                                   axis=0)  #add dimention that is batch_size

        pred = model.predict(img_array, verbose=0)

        print('prediction result : {}'.format(class_name[np.argmax(
            pred[0, :])]))
        print('correct answer : {}'.format(class_name[np.argmax(
            y_test[0, :])]))

    else:
        print('illegal input.')
        print('please select train or test')
def VGG16_Test(model, image_path, image_name, test_result):
    image_data = os.path.join(image_path, image_name)
    print(image_data)
    image_origin = cv2.imread(image_data)
    image = cv2.resize(image_origin, (224, 244))
    image = image.astype(np.float32)
    image = np.transpose(image, (2, 1, 0))
    image = torch.from_numpy(image).unsqueeze(0)

    if torch.cuda.is_available():
        image = image.cuda()

    out = model(image)
    pre = torch.max(out, 1)[1].cpu()
    pre = pre.numpy()
    print(classes[int(pre[0])])
    cv2.imwrite(test_result + str(classes[int(pre[0])]) + "_" + image_name,
                image_origin)


if __name__ == "__main__":
    model = VGG16.VGG16()
    model.load_state_dict(torch.load(model_path))
    if torch.cuda.is_available():
        model = model.cuda()

    data_list = os.listdir(image_path)
    for data in data_list:
        VGG16_Test(model, image_path, data, test_reuslt)
Beispiel #18
0
def VGG16_run():

    train_loss, train_acc = [], []
    valid_loss, valid_acc = [], []
    # load data
    mnist = input_data.read_data_sets("./data/mnist", one_hot=True)

    #reset graph
    tf.reset_default_graph()
    # Graph
    x = tf.placeholder(tf.float32, [None, img_size, img_size, img_channels],
                       name='x-input')
    y_ = tf.placeholder(tf.float32, [None, num_classes], name='y-input')

    training_phase = tf.placeholder(tf.bool, None, name='training_phase')
    keep_prob = tf.placeholder(tf.float32, None, name='keep_prob')

    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)

    y = VGG16.VGG_16(x, keep_prob, regularizer)

    global_step = tf.Variable(0, trainable=False)

    variable_averages = tf.train.ExponentialMovingAverage(
        MOVING_AVERAGE_DECAY, global_step)
    variable_averages_op = variable_averages.apply(tf.trainable_variables())

    # labels is the label index, not the values
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=tf.argmax(y_, 1), logits=y)
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
    loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
    #loss = cross_entropy_mean

    learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step,
                                               num_imges_train // BATCH_SIZE,
                                               LEARNING_RATE_DECAY)

    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        loss, global_step=global_step)
    #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    #grads = optimizer.compute_gradients(loss, var_list=tf.trainable_variables())
    #train_step=optimizer.apply_gradients(grads, global_step=global_step)

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

    with tf.control_dependencies([train_step, variable_averages_op]):
        train_op = tf.no_op(name="train")

    saver = tf.train.Saver()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        tf.global_variables_initializer().run()
        #start queue runner
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for epoch in range(epochs):
            NumOfBatchTrain = num_imges_train // BATCH_SIZE
            for i in range(NumOfBatchTrain):
                #train_x_batch = train_x[i*BATCH_SIZE:(i+1)*BATCH_SIZE, :, :,:]
                #train_y_batch = train_y[i*BATCH_SIZE:(i+1)*BATCH_SIZE,:]
                train_x_batch, train_y_batch = mnist.train.next_batch(
                    BATCH_SIZE)
                train_x_batch = np.reshape(
                    train_x_batch, (-1, img_size, img_size, img_channels))

                _, loss_value, step, acc_value = sess.run(
                    [train_op, loss, global_step, accuracy],
                    feed_dict={
                        x: train_x_batch,
                        y_: train_y_batch,
                        training_phase: True,
                        keep_prob: 0.5
                    })
                train_loss.append(loss_value)
                train_acc.append(acc_value)
                if (step - 1) % 100 == 0:
                    print(
                        "training steps: %d , training loss: %g, train accuracy: %g"
                        % (step, loss_value, acc_value))

            #validation in batch
            NumOfBatchValid = num_imges_valid // BATCH_SIZE
            _valid_loss, _valid_acc = [], []

            for i in range(NumOfBatchValid):
                valid_x_batch, valid_y_batch = mnist.test.next_batch(
                    BATCH_SIZE)
                valid_x_batch = np.reshape(
                    valid_x_batch, (-1, img_size, img_size, img_channels))
                loss_val_batch, accuracy_val_batch = sess.run(
                    [loss, accuracy],
                    feed_dict={
                        x: valid_x_batch,
                        y_: valid_y_batch,
                        training_phase: False,
                        keep_prob: 1.0
                    })
                _valid_loss.append(loss_val_batch)
                _valid_acc.append(accuracy_val_batch)
            valid_loss.append(np.mean(_valid_loss))
            valid_acc.append(np.mean(_valid_acc))
            print("validation accuracy: %g" % (valid_acc[-1]))
            if valid_acc[-1] > 0.5:
                saver.save(sess,
                           os.path.join(save_dir, MODEL_NAME),
                           global_step=global_step)

            # test
            '''test_acc = sess.run('accuracy:0', 
                             feed_dict={'x-input:0': test_x,
                                       'y-input:0': test_y,
                                       'training_phase:0': False})
            print("test accuracy: %g" % (test_acc))'''

        coord.request_stop()
        coord.join(threads)

        #save loss and accuracy data
        np.save(os.path.join(save_dir, 'accuracy_loss', 'train_loss'),
                train_loss)
        np.save(os.path.join(save_dir, 'accuracy_loss', 'train_acc'),
                train_acc)
        np.save(os.path.join(save_dir, 'accuracy_loss', 'valid_loss'),
                valid_loss)
        np.save(os.path.join(save_dir, 'accuracy_loss', 'valid_acc'),
                valid_acc)
Beispiel #19
0
if __name__ == '__main__':

    #在不同py文件中使用了tf.app.flags.DEFINE_XX函数,实际上是一个全局变量,可以整合,并保存在一个实例中,可以被不同的引用进行引用
    #在其他文件中也定义了tf.app.flags.DEFINE_XX 但是最终都会在一个变量中被使用

    # print(appFlags.my_name)
    # print(appFlags.height)

    trainModel = tf.placeholder(bool)

    image_batch, label_batch = read_and_decode("../Utils/cat_dog.tfrecords",
                                               32)
    #需要验证集 则需要另外调用read_and_decode 生成验证集的数据集合

    mode = VGG16.Vgg16(trainable=True,
                       trainModel=trainModel,
                       dropOutRatio=0.5,
                       vgg16_npy_path="./vgg16-save.npy")

    inputData = tf.placeholder(dtype=tf.float32, shape=[None, 244, 244, 3])
    inputLabel = tf.placeholder(dtype=tf.float32, shape=[None, 2])

    #建立网络
    mode.build(inputData)

    #得到训练步骤
    trainStep, loss, label = mode.trainOptimizer(inputLabel)

    #得到准确度
    accuracy = mode.calcAccuracy(inputLabel)

    #初始化所有操作
def inference(images, keep_prob, is_training):
  images = tf.reshape(images, [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 3])  # 256,256,3
  
  softmax_linear = VGG16.VGG16(images)
  return tf.reshape(softmax_linear, [-1, CLASSES_NUM])
def AlexNet_run():

    train_loss, train_acc = [], []
    valid_loss, valid_acc = [], []
    test_loss, test_acc = [], []
    # load data
    #Dataset
    class_name_file = 'class_name.txt'
    class_name_path = './data/quickDraw/' + class_name_file
    with open(class_name_path) as f:
        file_list = f.read().splitlines()
    total_x, _, total_y = load_data(file_list)
    total_x = np.reshape(total_x, (-1, img_size, img_size, img_channels))
    total_y = np.reshape(total_y, (-1, num_classes))

    ## Shuffling & train/validation split
    shuffle_idx = np.arange(total_y.shape[0])
    shuffle_rng = np.random.RandomState(123)
    shuffle_rng.shuffle(shuffle_idx)
    total_x, total_y = total_x[shuffle_idx], total_y[shuffle_idx]
    train_x, train_y = total_x[:int(num_images *
                                    (1 - validation_ratio - test_ratio)
                                    ), :, :, :], total_y[:int(num_images * (
                                        1 - validation_ratio - test_ratio)), :]
    valid_x, valid_y = total_x[int(num_images * (
        1 - validation_ratio -
        test_ratio)):int(num_images * (1 - test_ratio)), :, :, :], total_y[
            int(num_images *
                (1 - validation_ratio - test_ratio)):int(num_images *
                                                         (1 - test_ratio)), :]
    test_x, test_y = total_x[int(num_images *
                                 (1 - test_ratio)):, :, :, :], total_y[
                                     int(num_images * (1 - test_ratio)):, :]

    #reset graph
    tf.reset_default_graph()
    # Graph
    x = tf.placeholder(tf.float32, [None, img_size, img_size, img_channels],
                       name='x-input')
    y_ = tf.placeholder(tf.float32, [None, num_classes], name='y-input')

    training_phase = tf.placeholder(tf.bool, None, name='training_phase')
    keep_prob = tf.placeholder(tf.float32, None, name='keep_prob')

    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)

    y = VGG16.VGG_16(x, keep_prob, regularizer)

    global_step = tf.Variable(0, trainable=False)

    variable_averages = tf.train.ExponentialMovingAverage(
        MOVING_AVERAGE_DECAY, global_step)
    variable_averages_op = variable_averages.apply(tf.trainable_variables())

    # labels is the label index, not the values
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=tf.argmax(y_, 1), logits=y)
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
    loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
    #loss = cross_entropy_mean

    learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step,
                                               num_imges_train // BATCH_SIZE,
                                               LEARNING_RATE_DECAY)

    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        loss, global_step=global_step)
    #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    #minimize is an combi-operation of compute gradients and apply gradients
    #grads = optimizer.compute_gradients(loss, var_list=tf.trainable_variables())
    #train_step=optimizer.apply_gradients(grads, global_step=global_step)

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

    with tf.control_dependencies([train_step, variable_averages_op]):
        train_op = tf.no_op(name="train")

    saver = tf.train.Saver()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        tf.global_variables_initializer().run()
        #start queue runner
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for epoch in range(epochs):

            NumOfBatchTrain = int(num_imges_train) // BATCH_SIZE
            for i in range(NumOfBatchTrain):
                train_x_batch = train_x[i * BATCH_SIZE:(i + 1) *
                                        BATCH_SIZE, :, :, :]
                train_y_batch = train_y[i * BATCH_SIZE:(i + 1) * BATCH_SIZE, :]
                #train_x_batch, train_y_batch = mnist.train.next_batch(BATCH_SIZE)
                #train_x_batch=np.reshape(train_x_batch, (-1, img_size, img_size, img_channels))

                _, loss_train_batch, step, acc_train_batch = sess.run(
                    [train_op, loss, global_step, accuracy],
                    feed_dict={
                        x: train_x_batch,
                        y_: train_y_batch,
                        training_phase: True,
                        keep_prob: 0.5
                    })
                train_loss.append(loss_train_batch)
                train_acc.append(acc_train_batch)
                if (step - 1) % 100 == 0:
                    print(
                        "training steps: %d , training loss: %g, train accuracy: %g"
                        % (step, loss_train_batch, acc_train_batch))

            #validation in batch
            NumOfBatchValid = int(num_imges_valid) // BATCH_SIZE
            _valid_loss, _valid_acc = [], []

            for i in range(NumOfBatchValid):
                #valid_x_batch, valid_y_batch = mnist.test.next_batch(BATCH_SIZE)
                #valid_x_batch=np.reshape(valid_x_batch, (-1, img_size, img_size, img_channels))
                valid_x_batch = valid_x[i * BATCH_SIZE:(i + 1) *
                                        BATCH_SIZE, :, :, :]
                valid_y_batch = valid_y[i * BATCH_SIZE:(i + 1) * BATCH_SIZE, :]
                loss_val_batch, accuracy_val_batch = sess.run(
                    [loss, accuracy],
                    feed_dict={
                        x: valid_x_batch,
                        y_: valid_y_batch,
                        training_phase: False,
                        keep_prob: 1.0
                    })
                _valid_loss.append(loss_val_batch)
                _valid_acc.append(accuracy_val_batch)
            valid_loss.append(np.mean(_valid_loss))
            valid_acc.append(np.mean(_valid_acc))
            print("validation accuracy: %g" % (valid_acc[-1]))
            if valid_acc[-1] > 0.5:
                saver.save(sess,
                           os.path.join(save_dir, MODEL_NAME),
                           global_step=global_step)

            # test
            NumOfBatchTest = int(num_imges_test) // BATCH_SIZE
            _test_loss, _test_acc = [], []

            for i in range(NumOfBatchTest):
                test_x_batch = test_x[i * BATCH_SIZE:(i + 1) *
                                      BATCH_SIZE, :, :, :]
                test_y_batch = test_y[i * BATCH_SIZE:(i + 1) * BATCH_SIZE, :]
                loss_val_batch, accuracy_val_batch = sess.run(
                    [loss, accuracy],
                    feed_dict={
                        x: test_x_batch,
                        y_: test_y_batch,
                        training_phase: False,
                        keep_prob: 1.0
                    })
                _test_loss.append(loss_val_batch)
                _test_acc.append(accuracy_val_batch)
            test_loss.append(np.mean(_test_loss))
            test_acc.append(np.mean(_test_acc))
            print("test accuracy: %g" % (test_acc[-1]))

        coord.request_stop()
        coord.join(threads)

        #save loss and accuracy data
        Path(os.path.join(save_dir, 'accuracy_loss')).mkdir(parents=True,
                                                            exist_ok=True)
        np.save(os.path.join(save_dir, 'accuracy_loss', 'train_loss'),
                train_loss)
        np.save(os.path.join(save_dir, 'accuracy_loss', 'train_acc'),
                train_acc)
        np.save(os.path.join(save_dir, 'accuracy_loss', 'valid_loss'),
                valid_loss)
        np.save(os.path.join(save_dir, 'accuracy_loss', 'valid_acc'),
                valid_acc)
Beispiel #22
0
# 选择训练环境
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# 数据归一化
transform = transforms.Compose(
        [
            transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ]
)

# cifar10数据标签
cifar10_classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

checkpoint = torch.load('./checkpoint/cifar10_epoch_5.ckpt', map_location='cpu')
net = VGG16.VGG('VGG16').to(device)
net.load_state_dict(checkpoint['net'])
start_epoch = checkpoint['epoch']

if __name__ == '__main__':
    # 载入数据
    testset = torchvision.datasets.CIFAR10(
        root='./Cifar10',
        train=False,
        transform=transform,
        download=True
    )

    testloader = torch.utils.data.DataLoader(
        testset,
        num_workers=2,
def main(_):
    tf.logging.set_verbosity(tf.logging.INFO)
    image_lists = create_image_lists(FLAGS.image_dir, FLAGS.testing_percentage,
                                   FLAGS.validation_percentage)
    class_count = len(image_lists.keys())
    if class_count == 0:
        tf.logging.error('No valid folders of images found at ' + FLAGS.image_dir)
        return -1
    if class_count == 1:
        tf.logging.error('Only one valid folder of images found at ' +
                         FLAGS.image_dir +
                         ' - multiple classes are needed for classification.')
        return -1

    x = tf.placeholder(tf.float32, shape=[None, 224, 224, 3])
    y_ = tf.placeholder(tf.float32, shape=[None, class_count])
    logits = VGG16.vgg16_net(x,class_count)
    loss = caculateloss(logits, y_)
    acc = accuracy(logits, y_)
    my_global_step = tf.Variable(0, name='global_step', trainable=False)
    train_step = optimize(loss, FLAGS.learning_rate,my_global_step)

    #with graph.as_default():
    #     (train_step, cross_entropy,bottleneck_input,input_groud_truth) = train(output_tensor)
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        #print(tf.trainable_variables())
        load_with_skip('vgg16.npy', sess, ['fc6', 'fc7', 'fc8'])
        jpeg_data_tensor, decoded_image_tensor = add_jpeg_decoding()
        cache_bottleneck(sess, image_lists, FLAGS.image_dir,
                           FLAGS.cache_dir, jpeg_data_tensor,
                           decoded_image_tensor, x)


        # 评估预测准确率
        #evaluation_step, _ = add_evaluation_step(output_tensor, ground_truth_input)

        saver = tf.train.Saver(tf.global_variables())
        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train',
                                             sess.graph)
        validation_writer = tf.summary.FileWriter(
            FLAGS.summaries_dir + '/validation')
        train_saver = tf.train.Saver()

        for i in range(FLAGS.how_many_training_steps):
            (train_cached_tensor,
             train_ground_truth, _) = get_random_cached_bottlenecks(
                sess, image_lists, FLAGS.train_batch_size, 'training',
                FLAGS.cache_dir, FLAGS.image_dir, jpeg_data_tensor,
                decoded_image_tensor, x)
            _, tra_loss, tra_acc = sess.run([train_step,loss,acc],feed_dict={x:train_cached_tensor, y_:train_ground_truth})
            print('Step: %d, loss: %.4f, accuracy: %.4f%%' % (i, tra_loss, tra_acc))
            is_last_step = (i + 1 == FLAGS.how_many_training_steps)
            # 训练完成或每完成eval_step_interval各batch训练,打印准确率和交叉熵
            if (i % FLAGS.eval_step_interval) == 0 or is_last_step:
                (validation_cached_tensor,
                 validation_ground_truth, _) = get_random_cached_bottlenecks(
                    sess, image_lists, FLAGS.validation_batch_size, 'validation',
                    FLAGS.cache_dir, FLAGS.image_dir, jpeg_data_tensor,
                    decoded_image_tensor, x)
                val_loss, val_acc = sess.run([loss, acc],
                                             feed_dict={x: validation_cached_tensor, y_: validation_ground_truth})
                print('**  Step %d, val loss = %.2f, val accuracy = %.2f%%  **' % (i, val_loss, val_acc))
            if i % 2000 == 0 or is_last_step:
                checkpoint_path = os.path.join('model', 'model.ckpt')
                saver.save(sess, checkpoint_path,global_step=i)
Beispiel #24
0
from  VGG16 import *

parser = argparse.ArgumentParser()

parser.add_argument("--caffemodel",help = "path to trained caffe model",type = str,default = "VGG_ILSVRC_16_layers.caffemodel")
parser.add_argument("--deploy",help =" path to  deploy.protxt",type = str,default = "VGG_ILSVRC_16_layers_deploy.prototxt")
parser.add_argument("--image_path",help = "path to image",type = str,default = "ak.png")
#parser.add_argument("--mean_file",help = "path to mean image",type = str,default = "")
parser.add_argument("--output_file",help = "writing features to this file",type = str,default = "out.txt")
parser.add_argument("--extract_layer",help = "layer's name that extract features from",type = str,default = "conv1_1")


args = parser.parse_args()

vgg = VGG16(model_path = args.deploy,pretrained_path = args.caffemodel)

img = caffe.io.load_image(args.image_path)
features = vgg.extract_feature(img,blob = args.extract_layer)

features = np.resize(features,(1,len(features)))

if args.output_file.endswith(".npy"):
    np.save(args.output_file,features)
elif args.output_file.endswith(".txt"):
    np.savetxt(args.output_file,features,delimiter = "\t")

#print type(features)

print features.shape
Beispiel #25
0
def predict(image_path):
    image = PIL.Image.open(image_path)
    img_tensor = transform(image).unsqueeze_(0)
    # reference: https://github.com/pytorch/pytorch/issues/26338
    # functional dropout can't be turned off using net.eval()
    # they must be turned off manually
    net.eval()
    output = net(img_tensor)
    prob, index = torch.max(output, dim=1)
    return prob.item(), CLASSES[index.item()]

if __name__ == "__main__":
    # load the model
    FILEPATH = os.path.dirname(os.path.realpath(__file__)) + '/../model/'
    FILENAME = 'model-epoch10.pth'
    net = VGG16.Net(num_classes=2)
    # reference: https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-model-across-devices
    checkpoint = torch.load(FILEPATH + FILENAME, map_location=device)
    net.load_state_dict(checkpoint.get('net_state_dict'))
    # optimizer.load_state_dict(checkpoint.get('optimizer_state_dict'))
    print(f"Last validation accuracy: {checkpoint.get('valacc')}%\n")
    # next_epoch = checkpoint.get('epoch')

    # predict image
    IMAGE_PATH = os.path.dirname(os.path.realpath(__file__)) + '/../sample/'
    IMAGE_NAME = 'sample1.jpg'
    # image = PIL.Image.open(IMAGE_PATH + IMAGE_NAME)
    # image_tensor = transforms.ToTensor()(image)
    # print(image_tensor.shape)
    # plt.imshow(np.transpose(transforms.ToTensor()(PIL.Image.open(IMAGE_PATH + IMAGE_NAME)), (1,2,0)))
    # plt.show()
Beispiel #26
0
def train(train_record_file, train_log_step, train_param, val_record_file,
          val_log_step, labels_nums, data_format, snapshot, snapshot_prefix):
    '''
    :param train_record_file:
    :param train_log_step:
    :param train_param: train参数
    :param val_record_file:
    :param val_log_step:
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_format: 数据格式
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr, max_steps] = train_param
    [batch_size, resize_height, resize_width, depths] = data_format

    # 获得训练和测试的样本数
    train_nums = get_example_nums(train_record_file)
    val_nums = get_example_nums(val_record_file)
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))
    # 从record中读取图片和labels数据
    # train数据
    min_after_dequeue = 200
    capacity = min_after_dequeue + 3 * batch_size  # 保证capacity必须大于min_after_dequeue参数值
    train_images, train_labels = read_records(train_record_file,
                                              resize_height,
                                              resize_width,
                                              type='normalization')  # 读取函数
    train_images_batch, train_labels_batch = tf.train.shuffle_batch(
        [train_images, train_labels],
        batch_size=batch_size,
        capacity=capacity,
        min_after_dequeue=min_after_dequeue)
    train_labels_batch = tf.one_hot(train_labels_batch, labels_nums, 1, 0)

    # val数据
    val_images, val_labels = read_records(val_record_file,
                                          resize_height,
                                          resize_width,
                                          type='normalization')  # 读取函数
    # input_queue = tf.train.slice_input_producer([val_images,val_labels],shuffle=False,num_epochs=1)
    val_images_batch, val_labels_batch = tf.train.batch(
        [val_images, val_labels], batch_size=batch_size, capacity=capacity)
    val_labels_batch = tf.one_hot(val_labels_batch, labels_nums, 1, 0)

    # 建立网络,训练时keep_prob为0.5,测试时1.0
    output = VGG16(x, keep_prob, labels_nums)

    # 定义训练的loss函数。
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
    #train_step = tf.train.AdamOptimizer(learning_rate=0.1).minimize(loss)
    train_step = tf.train.GradientDescentOptimizer(
        learning_rate=base_lr).minimize(loss)
    pred = tf.argmax(output, 1)  #预测labels最大值下标
    truth = tf.argmax(y, 1)  #实际labels最大值下标
    accuracy = tf.reduce_mean(tf.cast(tf.equal(pred, truth), tf.float32))

    saver = tf.train.Saver()
    max_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())  # 就是这一行
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps + 1):
            batch_x, batch_y = sess.run(
                [train_images_batch, train_labels_batch])
            _, train_loss = sess.run([train_step, loss],
                                     feed_dict={
                                         x: batch_x,
                                         y: batch_y,
                                         keep_prob: 0.5
                                     })
            # train测试(这里仅测试训练集的一个batch)
            if i % train_log_step == 0:
                train_acc = sess.run(accuracy,
                                     feed_dict={
                                         x: batch_x,
                                         y: batch_y,
                                         keep_prob: 1.0
                                     })
                print "%s: Step [%d]  train Loss : %f, training accuracy :  %g" % (
                    datetime.now(), i, train_loss, train_acc)

            # val测试(测试全部val数据)
            if i % val_log_step == 0:
                mean_loss, mean_acc = net_evaluation(sess, loss, accuracy,
                                                     val_images_batch,
                                                     val_labels_batch,
                                                     val_nums)
                print "%s: Step [%d]  val Loss : %f, val accuracy :  %g" % (
                    datetime.now(), i, mean_loss, mean_acc)

            # 模型保存:每迭代snapshot次或者最后一次保存模型
            if (i % snapshot == 0 and i > 0) or i == max_steps:
                print('-----save:{}-{}'.format(snapshot_prefix, i))
                saver.save(sess, snapshot_prefix, global_step=i)
            # 保存val准确率最高的模型
            if mean_acc > max_acc and mean_acc > 0.5:
                max_acc = mean_acc
                path = os.path.dirname(snapshot_prefix)
                best_models = os.path.join(
                    path, 'best_models_{}_{:.4f}.ckpt'.format(i, max_acc))
                print('------save:{}'.format(best_models))
                saver.save(sess, best_models)

        coord.request_stop()
        coord.join(threads)
Beispiel #27
0
def main():
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=100,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.5,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging training status')
    args = parser.parse_args()
    print(args)

    use_cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)
    device = torch.device("cuda" if use_cuda else "cpu")

    PATH_TO_IMAGES = ""
    TRAIN_DATASET_CSV = ""
    TEST_DATASET_CSV = ""

    transform = transforms.Compose([
        transforms.Grayscale(),
        transforms.Resize((224, 224), interpolation=2),
        transforms.ToTensor()
    ])

    train_dataset = BoneDataset224(PATH_TO_IMAGES, TRAIN_DATASET_CSV,
                                   transform)
    test_dataset = BoneDataset224(PATH_TO_IMAGES, TEST_DATASET_CSV, transform)

    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=32,
                                               shuffle=True)

    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=64,
                                              shuffle=True)

    PATH_TO_WEIGHTS = "./weights/" + "ws_vgg16_sdg.pt"
    PATH_TO_WEIGHTS = None
    model = VGG16.vgg16(False, PATH_TO_WEIGHTS).to(device)
    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr,
                          momentum=args.momentum)

    for epoch in range(1, args.epochs + 1):
        train(args, model, device, train_loader, optimizer, epoch)
        test(args, model, device, test_loader)
        torch.save(model.state_dict(), PATH_TO_WEIGHTS)
                mixed_image, outputdir + '\\output' + filename_content + '_' +
                str(i) + '.jpg')

    # print()
    # print("Final image:")
    #plot_image_big(mixed_image)
    # plot_images(content_image=content_image,
    #             style_image=style_image,
    #             mixed_image=mixed_image)
    # save_image(mixed_image, outputdir + '\\final_output.jpg')

    # Return the mixed-image.
    return mixed_image


model = vgg16.VGG16()
#model = VGG16()

# Create a TensorFlow-session.

#Example

os.chdir("D:\\Python\\videostyle\\test_output")
for file in os.listdir(os.getcwd()):
    if file.endswith(".jpg"):

        print(file)

        content_filename = ''.join(file.rsplit('.', 1)[:1])

        content_filepath = 'D:\\Python\\videostyle\\test_output\\' + content_filename + '.jpg'