python - 执行程序未能创建内核。未实现:不支持将字符串转换为 int64
问题描述
我试图使用最新版本的 TensorFlow 训练包含 306 张食物图像的本地数据集。但是,当我尝试运行它时,它打印了
训练 306 个样本
纪元 1/5
此时它刚刚停止做任何事情,所以当我尝试在 cmd 中运行它时出现以下错误
Train on 306 samples
Epoch 1/5
2019-12-09 15:11:42.788897: E tensorflow/core/common_runtime/executor.cc:642] Executor failed to create kernel. Unimplemented: Cast string to int64 is not supported
[[{{node loss/activation_2_loss/Cast}}]]
我现在已经查看了 100 次代码,我似乎无法理解我在哪里弄错了,我也无法在任何其他帖子上找到解决方案。
现在看代码
我首先加载图像,调整它们的大小并分配标签:
TRAIN_DIR = './food11/training'
class_names=['Bread', 'Dairy product', 'Dessert', 'Egg', 'Fried food', 'Meat']
def load_training_data(DIR):
train_data = []
for img in os.listdir(DIR):
word_label = int(img.split('_')[0])
label = class_names[word_label]
path = os.path.join(DIR, img)
if "DS_Store" not in path:
img = Image.open(path)
img = img.convert('L')
img = img.resize((IMG_SIZE, IMG_SIZE), Image.ANTIALIAS)
train_data.append([np.array(img), label])
shuffle(train_data)
return train_data
train_data = load_training_data(TRAIN_DIR)
trainImages = np.array([i[0] for i in train_data]).reshape(-1, IMG_SIZE,IMG_SIZE, 1)
trainLabels = np.array([i[1] for i in train_data])
然后我对图像进行了归一化,并创建了一个包含 6 个神经元的小模型:
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(IMG_SIZE, IMG_SIZE, 1)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dense(6))
model.add(Activation("softmax"))
model.compile(loss="sparse_categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
model.fit(trainImages, trainLabels, batch_size = 50, epochs = 5, verbose = 1)
任何帮助将不胜感激。感谢您的时间!
解决方案
这个问题的解决方案是关于“标签”的一个小错误。
word_label = int(img.split('_')[0])
label = class_names[word_label]
它将 trainLabels 作为字符串返回(类名称而不是索引),所以我通过将上面的行替换为
label = int(img.split('_')[0])
推荐阅读
- python - 在python中使用递归函数
- c# - Expander 超出最大高度,无法添加 ScrollViewer
- netlogo - 在 Netlogo 中为补丁分配值
- java - 如何检查方法transferMoney(User fromUser,User toUser,int amount)中的货币是否不同?
- python - Python3,列表中的元组,如何获取其中的所有值
- c++ - 无法从 char 构造 CString
- mysql - Grails 以意想不到的额外精度创建日期时间列
- sql - 如何从 TABLE 中删除/删除 UNIQUE KEY 的效果
- apache-spark - Databricks Spark:java.lang.OutOfMemoryError:超过 GC 开销限制 i
- swift - Mojave 上的日期问题 Xcode 11、11.1 - 调试器矛盾