-3

I'm trying to train a large data of about 10,000 images using the vgg16 pre-trained network for this i coded this but it seems to generate

ValueError: too many values to unpack (expected 2).

path= "C:/Users/52/.spyder-py3/IAM/train_patches/*.png"

(X_train,y_train),(X_test,y_test) = path   //The error is occurring here

initially when i was just using it simply it was working but now when i'm using datagen function its not working. Please kindly help me in making this code correct

from keras.models import model_from_json
from keras.applications import VGG16
import numpy as np
import glob
import os
import keras
from keras.utils import to_categorical
from keras import backend as K
from PIL import Image
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from sklearn.model_selection import train_test_split
from keras import optimizers
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator


seed=10
np.random.seed(seed)

path= "C:/Users/52/.spyder-py3/IAM/train_patches/*.png"

(X_train,y_train) == path   //The error is occurring here
sample_image = X_train[1,:,:,:]
plt.imshow(sample_image), plt.axis('off')
plt.show()
classes = 651
Y_train = to_categorical(y_train,classes)

X_train = X_train.astype('float32')


X_train = X_train/255


img_rows, img_cols = 500,500
channels=3

#Include_top=False, Does not load the last two fully connected layers which act as the classifier.
#We are just loading the convolutional layers. 
vgg_conv = VGG16(weights='imagenet',include_top=False,input_shape=(img_rows,img_cols,3))

# freeze the layer except the last 4 layers
for layer in vgg_conv.layers[:-4]:
    layer.trainable=False
num_classes=10
model = Sequential() 
# Add the vgg convolutional base model
model.add(vgg_conv) 
# Add new layers
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
 # Show a summary of the model. Check the number of trainable parameters
model.summary()    
datagen = ImageDataGenerator(rotation_range=40,
                             width_shift_range=0.2,
                             height_shift_range=0.2,
                             zoom_range=0.2,   
                             horizontal_flip=True,
                             fill_mode='nearest')

datagen.fit(X_train)

print("Size is: ",X_train.shape[0])

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

#generator=datagen.flow(datagen.flow(X_train,Y_train,batch_size=128))

history = model.fit_generator(datagen.flow(X_train,Y_train,batch_size=128),
                              steps_per_epoch=X_train.shape[0]/128,
                              epochs = 2,
                              verbose=1)
acc = history.history['acc']
loss = history.history['loss']
epochs = range(len(acc)) 
plt.plot(epochs, acc, 'b', label='Training acc')
plt.title('Training accuracy')
plt.legend()
plt.show()
model_json = model.to_json()
open('C:/Users/52/.spyder-py3/IAM/imdata.json','w').write(model_json)
model.save_weights('C:/Users/52/.spyder-py3/IAM/imdata.h5',overwrite=True)
Lily Swan
  • 1
  • 2
  • 1
    You really need to read [mcve], two lines would have been enough to demonstrate your problem. Also, reconsider your question's title thereafter. As new user here, please take the [tour] and read [ask] as well. Oh, and read https://stackoverflow.com/questions/1479776/too-many-values-to-unpack-exception?r=SearchResults&s=1|290.6034. That's the first hit when just searching the error message. – Ulrich Eckhardt Dec 15 '19 at 09:34
  • You have a big conceptual misunderstanding, the line that produces your error makes no sense, you need a function that loads the data and makes a train/test split – Dr. Snoopy Dec 15 '19 at 17:41

1 Answers1

0

The error means that you're trying to make an assignment on 4 variables, but the function to thr right of the = only outputs two. I think it's beacuse you're reading the data and labels and also making training/testing split.

Try to read all the images and labels in only two variables and then make the split.