логиты и метки должны транслироваться: logits_size = [32,60] labels_size = [32,131] - PullRequest
0 голосов
/ 11 июля 2020

Я хочу классифицировать данные по фруктам с помощью cnn. и когда я подбираю модель, используя классификатор после создания всех слоев, я получаю эту ошибку в разделе «history = classifier ...» ниже. которые я не могу решить последние два дня. Я много искал, но, похоже, это не работает. PS: Любые подсказки также будут оценены.

вот мой код:

# -*- coding: utf-8 -*-
"""Fruits.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1AiGMVBshT9w-5T3fzzi3jyrJr0CVGv6A
"""

from google.colab import files

!pip install -q kaggle

uploaded = files.upload()

!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!ls ~/.kaggle
!chmod 600 /root/.kaggle/kaggle.json

!kaggle kernels list — user hinaaslam1 — sort-by dateRun

!kaggle datasets download -d moltean/fruits 

#https://www.kaggle.com/moltean/fruits see the dataset at this link.

!unzip -q fruits.zip -d content

!ls

!pip install --upgrade keras
!pip install tensorflow

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image



no_of_classes = 60

# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (5, 5), input_shape = (100, 100, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2), strides= 2))

# Adding a convolutional layer
classifier.add(Conv2D(64, (5, 5), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2), strides= 2))

# Adding a second convolutional layer
classifier.add(Conv2D(128, (5, 5), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2), strides= 2))

# Adding a third convolutional layer
classifier.add(Conv2D(256, (5, 5), activation = 'relu'))
classifier.add(Dropout(0.25))
classifier.add(MaxPooling2D(pool_size = (2, 2), strides= 2))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.25))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.25))
classifier.add(Dense(units = no_of_classes, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

Training = train_datagen.flow_from_directory('/content/fruits-360/Training',
                                                 target_size = (100, 100),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

Validation = test_datagen.flow_from_directory('/content/fruits-360/Test',
                                            target_size = (100, 100),
                                            batch_size = 32,
                                            class_mode = 'categorical')

"""# New Section"""

history = classifier.fit_generator(Training,
                         steps_per_epoch = 4000,
                         epochs = 25,
                         validation_data = Validation,
                         verbose = 1,
                         validation_steps = 4000)
classifier.summary()

tf.keras.models.save_model(
    model,
    filepath,
    overwrite=True,
    include_optimizer=True,
    save_format="tf",
    signatures=None
)
model.save("fruit.h5")

#Plotting graphs
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
 
epochs = range(len(acc))
 
plt.plot(epochs, acc, 'b', label='Training acc')
plt.plot(epochs, val_acc, 'r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
 
plt.figure()
 
plt.plot(epochs, loss, 'b', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
 
plt.show()

name_of_classes = [ "Apple Braeburn", "Apple Golden 1", "Apple Golden 2", "Apple Golden 3" , "Apple Granny Smith" ,"Apple Red 1" ,"Apple Red 2" ,"Apple Red 3" ,"Apple Red Delicious" ,"Apple Red Yellow" ,"Apricot" ,"Avocado","Avocado ripe" ,"Banana" ,"Banana Red" ,"Cactus fruit" ,"Carambula" ,"Cherry" ,"Clementine" ,"Cocos" ,"Dates" ,"Granadilla" ,"Grape Pink" ,"Grape White" ,"Grape White 2" ,"Grapefruit Pink" ,"Grapefruit White" ,"Guava" ,"Huckleberry" ,"Kaki" ,"Kiwi" ,"Kumquats" ,"Lemon" ,"Lemon Meyer" ,"Limes" ,"Litchi" ,"Mandarine" ,"Mango" ,"Maracuja" ,"Nectarine" ,"Orange" ,"Papaya" ,"Passion Fruit" ,"Peach" ,"Peach Flat" ,"Pear" ,"Pear Abate" ,"Pear Monster" ,"Pear Williams" ,"Pepino" ,"Pineapple" ,"Pitahaya Red" ,"Plum" ,"Pomegranate" ,"Quince" ,"Raspberry" ,"Salak" ,"Strawberry" ,"Tamarillo" , "Tangelo"]

test_image = image.load_img('litchi.jpg', target_size = (100, 100))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
Training.class_indices
img = 'litchi.jpg'
Image.open(img)

test_image = image.load_img('banana.jpg', target_size = (100, 100))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
Training.class_indices
img = 'banana.jpg'
Image.open(img)

test_image = image.load_img('lmn.jpg', target_size = (100, 100))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
Training.class_indices
img = 'lmn.jpg'
Image.open(img)

test_image = image.load_img('hb.jpg', target_size = (100, 100))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
Training.class_indices
img = 'hb.jpg'
Image.open(img)

test_image = image.load_img('lmn.jpg', target_size = (100, 100))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
Training.class_indices
img = 'lmn.jpg'
Image.open(img)
...