1

我正在尝试运行二进制图像分类器。

我的火车 CSV 文件有 4 列:

id, type, Good, Unusual

abc, ['Good'], 1, 0

我的代码如下:

path = ""

os.chdir(path)

train = pd.read_csv("binary_train.csv")

train_image = []

for i in tqdm(range(train.shape[0])):
    img = image.load_img(train['id'][i],target_size=(400,400,3))
    img = image.img_to_array(img)
    img = img/255
    train_image.append(img)
X = np.array(train_image)

#plt.imshow(X[2])

y = np.array(train.drop(['id', 'type'],axis=1))
#y.shape

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.1)

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(400,400,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy', metrics=['accuracy'])

model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test), batch_size=32)

我收到以下错误:

回溯(最后一次调用):文件“”,第 2 行,文件“C:\Users\yasir.pirkani\PycharmProjects\untitled\venv1\lib\site-packages\keras\engine\training.py”,第 1154 行,适合 batch_size=batch_size) 文件“C:\Users\yasir.pirkani\PycharmProjects\untitled\venv1\lib\site-packages\keras\engine\training.py”,第 621 行,在 _standardize_user_data exception_prefix='target')文件“C:\Users\yasir.pirkani\PycharmProjects\untitled\venv1\lib\site-packages\keras\engine\training_utils.py”,第 145 行,在 standardize_input_data str(data_shape)) ValueError:检查目标时出错:预期activation_5 具有形状 (1,) 但得到了形状 (2,) 的数组

我需要帮助来解决这个问题。

4

1 回答 1

2

当我进行二进制分类时,必须在最后一个密集层中进行更改。

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('sigmoid')) 
于 2020-08-21T07:32:27.787 回答