问题描述
tensorflow python framework erros invalidArgmentError:connot batch tensors with different shapes in componet 0. First element had shape [227,227,3] and element 19 had shape [227,227,4]
解决办法:数据集图像格式不对;convert(‘RGB’)
解决代码:
import numpy as np
from PIL import Image
import os
import cv2
directory_name='./flower_data/flower_photos/321'
for filename in os.listdir(directory_name):
'''
# 用os库读取文件夹内所有图片名称以操作
img1 = cv2.imread(directory_name + "/" + filename)
# print(filename)
c = img1.shape[2]
if c == 1:
print('*'+filename)
# img = Image.open(directory_name + "/" + filename)
img2 = img1[:, :, ::-1]
cv2.imwrite('./flower_data_rgb/flower_photos/321/{}'.format(filename), img2)
'''
'''
b,g,r=cv2.split(img)
img2=cv2.merge([r.g.b])
'''
img = Image.open(directory_name+'/'+filename)
if (img.mode != 'RGB'):
img = img.convert('RGB')
img.save('./flower_data_rgb/flower_photos/321/{}'.format(filename))
参考文章:https://blog.csdn.net/m0_37712157/article/details/87102507
代码部分找不到参考了部分大佬,找不到链接了 ,不过还是表示感谢。
其中,注释掉部分代码采用的使cv2转换成RGB ,转换成之后图像背景会出现变色,变黄或者变红,暂时为找到原因。因此注掉了。