Tensorflow:在我自己的图像上创建数据集/测试

时间:2019-08-01 21:23:07

标签: python tensorflow

如何调整代码以从目录导入原始图像,然后训练并测试原始图像?

我已经使用train(),classify()和main()函数创建了一个程序。

main()仅询问您是否要运行train()和classify()函数。

train()导入指定的数据集(到目前为止,我已使用mnist手写数字和fashion_mnist),设置训练模型并保存模型。

classify()加载train()保存的模型,对图像进行混洗,然后迭代数据集以进行预测。

如果您在tensorflow 1.14上测试代码,它应该可以正常工作。

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from random import shuffle

data_set = tf.keras.datasets.fashion_mnist
model_data = 'fashion_mnist.model'
image_size = 28
class_names = {0:'t-shirt',1:'trouser',2:'pullover',3:'dress',4:'coat',5:'sandal',6:'shirt',7:'sneaker',8:'bag',9:'boot'}

def train():
    (train_images, train_labels), (test_images, test_labels) = data_set.load_data()
    train_images = tf.keras.utils.normalize(train_images, axis = 1) #makes data value 0-1
    test_images = tf.keras.utils.normalize(test_images, axis = 1) #makes data value 0-1

    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Flatten(input_shape = (image_size, image_size))) #flattens image
    model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu)) #amount of neurons
    model.add(tf.keras.layers.Dense(10, activation = tf.nn.softmax)) #probability distr

    model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
    model.fit(train_images, train_labels, epochs = 5)

    val_loss, val_acc = model.evaluate(test_images, test_labels)
    print(val_loss, val_acc)
    model.save(model_data)

def classify():
    (train_images, train_labels), (test_images, test_labels) = data_set.load_data()
    train_images = tf.keras.utils.normalize(train_images, axis = 1) #makes data value 0-1
    test_images = tf.keras.utils.normalize(test_images, axis = 1) #makes data value 0-1

    shuffle(test_images)

    predict = 0
    for images in test_images:
        plt.imshow(test_images[predict], cmap = plt.cm.binary)
        plt.show()

        new_model = tf.keras.models.load_model(model_data)
        predictions = new_model.predict([test_images])
        predict_class = class_names[np.argmax(predictions[predict])]
        print("\nprediction: ", predict_class)
        predict += 1

def main():
    run_train = int(input("\nrun training? yes:1 | no:0 "))
    if run_train == 1:
        train()
    else:
        pass

    classify_images = int(input("classify? yes:1 | no:0 "))
    if classify_images == 1:
        classify()
    else:
        pass

main()

我想修改代码,以便变量“ data_set”指向具有以class_names命名的子目录的目录,例如

animals > dogs  > dog1.jpg, dog2.jpg ...
        > cats  > cat1.jpg, cat2.jpg ...
        > birds > bird1.jpg, bird2.jpg ...

每个jpg图像和标签数据都需要存储在类似于MNIST数据集的numpy数组中。所有这些可能都需要create_data_set()函数。

最后,需要对classify()函数进行调整以测试目录中的原始图像。

我知道这是很多问题。在过去的一周中,我尝试了数十种教程并阅读了许多文档,但是在导入自定义图像数据集以训练张量流模型和测试图像分类器方面我一头雾水。请帮助您获取任何信息或我可以开始的地方!谢谢!

0 个答案:

没有答案