Python Tensorflow数据被分为1类
你好,飞越者 我目前正在努力解决代码中的一个错误。 当我运行代码时,出于某种原因,它将训练图像分类到一个类中,这违背了代码的目的。我似乎找不到它这样做的原因。你能帮帮我吗? 这可能是新手犯的错误,对此我深表歉意 这是我的密码:Python Tensorflow数据被分为1类,python,machine-learning,Python,Machine Learning,你好,飞越者 我目前正在努力解决代码中的一个错误。 当我运行代码时,出于某种原因,它将训练图像分类到一个类中,这违背了代码的目的。我似乎找不到它这样做的原因。你能帮帮我吗? 这可能是新手犯的错误,对此我深表歉意 这是我的密码: from tensorflow import keras import pandas as pd from keras.models import load_model from keras.utils import CustomObjectScope from ker
from tensorflow import keras
import pandas as pd
from keras.models import load_model
from keras.utils import CustomObjectScope
from keras.initializers import glorot_uniform
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
line = "__________________________________"
data_x = os.path.join('/tmp/MyData/car-or-no-car/car')
data_y = os.path.join('/tmp/MyData/car-or-no-car/no-car')
print('Total training x images:', len(os.listdir(data_x)))
print('Total training y images:', len(os.listdir(data_y)))
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('acc')>0.9999):
print("Accuracy has reached 99.99 percent so cancelling training!")
self.model.stop_training = True
train_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(
'/tmp/MyData/car-or-no-car', # This is the source directory for training images
target_size=(300, 300), # All images will be resized to 150x150
batch_size=128,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
callbacks = myCallback()
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(300, 300, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
from tensorflow.keras.optimizers import RMSprop
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(lr=0.001),
metrics=['acc'])
train_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(
'/tmp/MyData/', # This is the source directory for training images
target_size=(300, 300), # All images will be resized to 150x150
batch_size=128,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
history = model.fit_generator(
train_generator,
steps_per_epoch=8,
epochs=10,
verbose=1)
print(line)
model.save('testing.h5')
print("The Model has been Saved!")
print(line)