美文网首页
tensorflow水果识别

tensorflow水果识别

作者: 花盆有话说 | 来源:发表于2021-07-07 16:53 被阅读0次

    问题

    有一组水果的训练集,我们对模型进行训练,思路跟之前我们识别猫与狗一样。

    设计解决这个问题的思路

    1、下载与放置训练图片
    2、现在对应的依赖,tensorflow、numpy等等
    3、构建训练集合
    4、建模
    5、对模型进行训练
    6、用测试模型进行验证
    7、输出结果
    8、优化模型 to step4
    

    [1]图片地址

    https://www.kaggle.com/moltean/fruits 现在数据,现在速度比较慢,可以使用网盘。

    网盘地址(提取码:a9wr)

    目录结构

    【2】处理训练集的数据结构

    import os
    import pandas as pd
    
    train_dir = './Training/'
    test_dir = './Test/'
    fruits = []
    fruits_image = []
    
    for i in os.listdir(train_dir):
        for image_filename in os.listdir(train_dir + i):
            fruits.append(i) # name of the fruit
            fruits_image.append(i + '/' + image_filename)
    train_fruits = pd.DataFrame(fruits, columns=["Fruits"])
    train_fruits["Fruits Image"] = fruits_image
    
    print(train_fruits)
    

    结果输出

               Fruits              Fruits Image
    0        Tomato 4    Tomato 4/r_236_100.jpg
    1        Tomato 4      Tomato 4/247_100.jpg
    2        Tomato 4      Tomato 4/257_100.jpg
    3        Tomato 4     Tomato 4/r_78_100.jpg
    4        Tomato 4     Tomato 4/r_68_100.jpg
    ...           ...                       ...
    67687  Peach Flat    Peach Flat/220_100.jpg
    67688  Peach Flat  Peach Flat/r_127_100.jpg
    67689  Peach Flat    Peach Flat/156_100.jpg
    67690  Peach Flat  Peach Flat/r_137_100.jpg
    67691  Peach Flat    Peach Flat/146_100.jpg
    

    【3】构造模型

    import matplotlib.pyplot as plt
    import seaborn as sns
    from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
    from glob import glob
    from keras.models import Sequential
    from keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense
    img = load_img(train_dir + "Cantaloupe 1/r_234_100.jpg")
    plt.imshow(img)
    plt.axis("off")
    plt.show()
    
    array_image = img_to_array(img)
    
    # shape (100,100)
    print("Image Shape --> ", array_image.shape)
    
    # 131个类目
    fruitCountUnique = glob(train_dir + '/*' )
    numberOfClass = len(fruitCountUnique)
    print("How many different fruits are there --> ",numberOfClass)
    
    # 构建模型
    model = Sequential()
    model.add(Conv2D(32,(3,3),input_shape = array_image.shape))
    model.add(Activation("relu"))
    model.add(MaxPooling2D())
    model.add(Conv2D(32,(3,3)))
    model.add(Activation("relu"))
    model.add(MaxPooling2D())
    model.add(Conv2D(64,(3,3)))
    model.add(Activation("relu"))
    model.add(MaxPooling2D())
    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation("relu"))
    model.add(Dropout(0.5))
    
    # 区分131类
    model.add(Dense(numberOfClass)) # output
    model.add(Activation("softmax"))
    model.compile(loss = "categorical_crossentropy",
    
                  optimizer = "rmsprop",
    
                  metrics = ["accuracy"])
    
    print("Target Size --> ", array_image.shape[:2])
    

    【4】训练模型

    train_datagen = ImageDataGenerator(rescale= 1./255,
                                       shear_range = 0.3,
                                       horizontal_flip=True,
                                       zoom_range = 0.3)
    
    test_datagen = ImageDataGenerator(rescale= 1./255)
    epochs = 100
    batch_size = 32
    train_generator = train_datagen.flow_from_directory(
                    train_dir,
                    target_size= array_image.shape[:2],
                    batch_size = batch_size,
                    color_mode= "rgb",
                    class_mode= "categorical")
    
    test_generator = test_datagen.flow_from_directory(
                    test_dir,
                    target_size= array_image.shape[:2],
                    batch_size = batch_size,
                    color_mode= "rgb",
                    class_mode= "categorical")
    
    for data_batch, labels_batch in train_generator:
        print("data_batch shape --> ",data_batch.shape)
        print("labels_batch shape --> ",labels_batch.shape)
        break
    
    hist = model.fit_generator(
            generator = train_generator,
            steps_per_epoch = 1600 // batch_size,
            epochs=epochs,
            validation_data = test_generator,
            validation_steps = 800 // batch_size)
    
    #保存模型 model_fruits.h5
    model.save('model_fruits.h5')
    

    【5】展示训练结果

    #展示损失模型结果
    plt.figure()
    plt.plot(hist.history["loss"],label = "Train Loss", color = "black")
    plt.plot(hist.history["val_loss"],label = "Validation Loss", color = "darkred", linestyle="dashed",markeredgecolor = "purple", markeredgewidth = 2)
    plt.title("Model Loss", color = "darkred", size = 13)
    plt.legend()
    plt.show()
    
    #展示精确模型结果
    plt.figure()
    plt.plot(hist.history["accuracy"],label = "Train Accuracy", color = "black")
    plt.plot(hist.history["val_accuracy"],label = "Validation Accuracy", color = "darkred", linestyle="dashed",markeredgecolor = "purple", markeredgewidth = 2)
    plt.title("Model Accuracy", color = "darkred", size = 13)
    plt.legend()
    plt.show()
    

    损失模型

    验证模型

    验证

    新建一个文件,用于验证训练出来的模型

    from tensorflow.keras.models import load_model
    import os
    import pandas as pd
    
    from keras.preprocessing.image import ImageDataGenerator,img_to_array, load_img
    import cv2,matplotlib.pyplot as plt,numpy as np
    from keras.preprocessing import image
    
    train_datagen = ImageDataGenerator(rescale= 1./255,
                                        shear_range = 0.3,
                                        horizontal_flip=True,
                                        zoom_range = 0.3)
    
    model = load_model('model_fruits.h5')
    batch_size = 32
    img = load_img("./Test/Apricot/3_100.jpg",target_size=(100,100))
    plt.imshow(img)
    plt.show()
    
    array_image = img_to_array(img)
    array_image = array_image * 1./255
    x = np.expand_dims(array_image, axis=0)
    images = np.vstack([x])
    classes = model.predict_classes(images, batch_size=10)
    print(classes)
    train_dir = './Training/'
    
    train_generator = train_datagen.flow_from_directory(
            train_dir,
            target_size= array_image.shape[:2],
            batch_size = batch_size,
            color_mode= "rgb",
            class_mode= "categorical”)
    print(train_generator.class_indices)
    

    输出结果

    [13]
    
    Found 67692 images belonging to 131 classes.
    
    {'Apple Braeburn': 0, 'Apple Crimson Snow': 1, 'Apple Golden 1': 2, 'Apple Golden 2': 3, 'Apple Golden 3': 4, 'Apple Granny Smith': 5, 'Apple Pink Lady': 6, 'Apple Red 1': 7, 'Apple Red 2': 8, 'Apple Red 3': 9, 'Apple Red Delicious': 10, 'Apple Red Yellow 1': 11, 'Apple Red Yellow 2': 12, 'Apricot': 13, 'Avocado': 14, 'Avocado ripe': 15, 'Banana': 16, 'Banana Lady Finger': 17, 'Banana Red': 18, 'Beetroot': 19, 'Blueberry': 20, 'Cactus fruit': 21, 'Cantaloupe 1': 22, 'Cantaloupe 2': 23, 'Carambula': 24, 'Cauliflower': 25, 'Cherry 1': 26, 'Cherry 2': 27, 'Cherry Rainier': 28, 'Cherry Wax Black': 29, 'Cherry Wax Red': 30, 'Cherry Wax Yellow': 31, 'Chestnut': 32, 'Clementine': 33, 'Cocos': 34, 'Corn': 35, 'Corn Husk': 36, 'Cucumber Ripe': 37, 'Cucumber Ripe 2': 38, 'Dates': 39, 'Eggplant': 40, 'Fig': 41, 'Ginger Root': 42, 'Granadilla': 43, 'Grape Blue': 44, 'Grape Pink': 45, 'Grape White': 46, 'Grape White 2': 47, 'Grape White 3': 48, 'Grape White 4': 49, 'Grapefruit Pink': 50, 'Grapefruit White': 51, 'Guava': 52, 'Hazelnut': 53, 'Huckleberry': 54, 'Kaki': 55, 'Kiwi': 56, 'Kohlrabi': 57, 'Kumquats': 58, 'Lemon': 59, 'Lemon Meyer': 60, 'Limes': 61, 'Lychee': 62, 'Mandarine': 63, 'Mango': 64, 'Mango Red': 65, 'Mangostan': 66, 'Maracuja': 67, 'Melon Piel de Sapo': 68, 'Mulberry': 69, 'Nectarine': 70, 'Nectarine Flat': 71, 'Nut Forest': 72, 'Nut Pecan': 73, 'Onion Red': 74, 'Onion Red Peeled': 75, 'Onion White': 76, 'Orange': 77, 'Papaya': 78, 'Passion Fruit': 79, 'Peach': 80, 'Peach 2': 81, 'Peach Flat': 82, 'Pear': 83, 'Pear 2': 84, 'Pear Abate': 85, 'Pear Forelle': 86, 'Pear Kaiser': 87, 'Pear Monster': 88, 'Pear Red': 89, 'Pear Stone': 90, 'Pear Williams': 91, 'Pepino': 92, 'Pepper Green': 93, 'Pepper Orange': 94, 'Pepper Red': 95, 'Pepper Yellow': 96, 'Physalis': 97, 'Physalis with Husk': 98, 'Pineapple': 99, 'Pineapple Mini': 100, 'Pitahaya Red': 101, 'Plum': 102, 'Plum 2': 103, 'Plum 3': 104, 'Pomegranate': 105, 'Pomelo Sweetie': 106, 'Potato Red': 107, 'Potato Red Washed': 108, 'Potato Sweet': 109, 'Potato White': 110, 'Quince': 111, 'Rambutan': 112, 'Raspberry': 113, 'Redcurrant': 114, 'Salak': 115, 'Strawberry': 116, 'Strawberry Wedge': 117, 'Tamarillo': 118, 'Tangelo': 119, 'Tomato 1': 120, 'Tomato 2': 121, 'Tomato 3': 122, 'Tomato 4': 123, 'Tomato Cherry Red': 124, 'Tomato Heart': 125, 'Tomato Maroon': 126, 'Tomato Yellow': 127, 'Tomato not Ripened': 128, 'Walnut': 129, 'Watermelon': 130}
    

    识别出是13:Apricot

    进一步验证

    fig = plt.figure(figsize=(16, 16))
    axes = []
    files = []
    predictions = []
    true_labels = []
    rows = 5
    cols = 2
    
    # 随机选择几个图片
    def getRandomImage(path, img_width, img_height):
        """function loads a random image from a random folder in our test path"""
        folders = list(filter(lambda x: os.path.isdir(os.path.join(path, x)), os.listdir(path)))
        random_directory = np.random.randint(0, len(folders))
        path_class = folders[random_directory]
        file_path = os.path.join(path, path_class)
        file_names = [f for f in os.listdir(file_path) if os.path.isfile(os.path.join(file_path, f))]
        random_file_index = np.random.randint(0, len(file_names))
        image_name = file_names[random_file_index]
        final_path = os.path.join(file_path, image_name)
        return image.load_img(final_path, target_size = (img_width, img_height)), final_path, path_class
    
    def draw_test(name, pred, im, true_label):
        BLACK = [0, 0, 0]
        expanded_image = cv2.copyMakeBorder(im, 160, 0, 0, 300, cv2.BORDER_CONSTANT, value=BLACK)
        cv2.putText(expanded_image, "predicted: " + pred, (20, 60), cv2.FONT_HERSHEY_SIMPLEX,
            0.85, (255, 0, 0), 2)
        cv2.putText(expanded_image, "true: " + true_label, (20, 120), cv2.FONT_HERSHEY_SIMPLEX,
            0.85, (0, 255, 0), 2)
        return expanded_image
    IMG_ROWS, IMG_COLS = 100, 100
    
    # predicting images
    for i in range(0, 10):
        path = "./Test"
        img, final_path, true_label = getRandomImage(path, IMG_ROWS, IMG_COLS)
        files.append(final_path)
        true_labels.append(true_label)
        x = image.img_to_array(img)
        x = x * 1./255
        x = np.expand_dims(x, axis=0)
        images = np.vstack([x])
        classes = model.predict_classes(images, batch_size=10)
        predictions.append(classes)
    
    class_labels = train_generator.class_indices
    class_labels = {v: k for k, v in class_labels.items()}
    class_list = list(class_labels.values())
    
    for i in range(0, len(files)):
        image = cv2.imread(files[i])
        image = draw_test("Prediction", class_labels[predictions[i][0]], image, true_labels[i])
        axes.append(fig.add_subplot(rows, cols, i+1))
        plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        plt.grid(False)
        plt.axis('off')
    plt.show()
    

    呈现结果

    水果识别

    相关文章

      网友评论

          本文标题:tensorflow水果识别

          本文链接:https://www.haomeiwen.com/subject/pzzoultx.html