ValueError:以10为基数的int()的文本无效:“crutyFree”

2024-07-04 05:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个训练神经网络的代码。这是我得到的错误

Traceback (most recent call last):

  File "/Users/ruthstam/Documents/Master/blok 3/SAT/untitled9.py", line 123, in <module>
    y_train = to_categorical(y_train, numofSamples)

  File "/Users/ruthstam/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/utils/np_utils.py", line 69, in to_categorical
    y = np.array(y, dtype='int')

ValueError: invalid literal for int() with base 10: 'cruelty-free'

我已经尝试通过将numofSamples转换为str来解决这个问题,并研究类似的问题。由于某种原因,我无法解决它。我认为这个错误是由to_分类行引起的

什么是可能的解决方案?有什么建议吗

代码如下:

for file in myList: 
        #print(len(np.where(y_train== file)[0]))
        numofSamples.append(len(np.where(y_train== file)[0]))

# preprossesing image 
def preProcessing(img):
    img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img = cv2.equalizeHist(img)
    img = img/255
    return img

X_train = np.array(list(map(preProcessing, X_train)))
X_test = np.array(list(map(preProcessing, X_test)))
X_validation = np.array(list(map(preProcessing, X_validation)))

# add depth of one, define parameters
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2],1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2],1)
X_validation = X_validation.reshape(X_validation.shape[0], X_validation.shape[1], X_validation.shape[2],1)


# generate images, provide different shapes
dataGen = ImageDataGenerator(width_shift_range=0.1,
                             height_shift_range=0.1,
                             zoom_range=0.2,
                             shear_range=0.1,
                             rotation_range=10)

# show dataset to generator
dataGen.fit(X_train)

y_train = to_categorical(y_train, numofSamples)
y_test = to_categorical(y_test, numofSamples)
y_validation = to_categorical(y_validation, numofSamples)

# create model
def myModel():
    noofFilters = 60
    sizeofFilter1 = (5,5)
    sizeofFilter2 = (3,3)
    sizeofPool = (2,2)
    noofNode = 500
    
    model = Sequential()
    model.add((Conv2D(noofFilters,sizeofFilter1,input_shape=(imageDimensions[0],
                                                             imageDimensions[1],
                                                             1), activation='relu'
                                                                 )))
    model.add((Conv2D(noofFilters,sizeofFilter1, activation='relu')))
    model.add(MaxPooling2D(pool_size=sizeofPool))
    model.add((Conv2D(noofFilters//2,sizeofFilter2, activation='relu')))
    model.add((Conv2D(noofFilters//2,sizeofFilter2, activation='relu')))
    model.add(MaxPooling2D(pool_size=sizeofPool))
    model.add(Dropout(0.5))
    
    model.add(Flatten())
    model.add(Dense(noofNode,activation='relu'))
    #reduce overfitting, with dropout
    model.add(Dropout(0.5))
    model.add(Dense(str(numofSamples),activation='softmax'))
    model.compile(Adam(lr=0.001),loss='categorical_crossentropy', metrics=['accuracy'])
    
    return model

model = myModel()
print(model.summary())

Tags: totestaddimgmodelnprangetrain

热门问题