I'm a newbei at Data Visualization, Machine Learning and Deep Learning topics. I'm trying to improve myself, but I stucked a topic when I try to implement a visualization.
In details, I tried to implement a kernel on 'Kaggle', my data is Cat and Dog images. I have train and test datas. The problem is that my train data has too many pictures that are different size, pixels and shapes. I want to make all of them in a 1 shape type.(For example, I want to make all of the pictures as 64x64 pixels, or 128x128 etc.)
""" I tried different codes to adjust its shapes and create plot:
I tried to reach 2 goals:
1. Convert the dog and cat images from RGB to Grayscale
2. Make all of the images 128x128, or 64x64 """
# One of the codes I've tried
img_size = 128
basewidth = 128
for image in tqdm(os.listdir(train_cat)):
path = os.path.join(train_cat, image)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
try:
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
except:
pass
np_img=np.asarray(img)
for image2 in tqdm(os.listdir(train_dog)):
path = os.path.join(train_dog, image2)
img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
try:
wpercent = (basewidth / float(img2.size[0]))
hsize = int((float(img2.size[1]) * float(wpercent)))
img2 = img2.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
except:
pass
np_img2=np.asarray(img2)
plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(np_img.reshape(img_size, img_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np_img2.reshape(img_size, img_size))
plt.axis('off')
# ---------------------------------------------------------------------
# Another way:
image_size = 128
for image in tqdm(os.listdir(train_dog)):
path = os.path.join(train_dog, image)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (image_size, image_size)).flatten()
np_img=np.asarray(img)
for image2 in tqdm(os.listdir(train_cat)):
path = os.path.join(train_cat, image2)
img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2, (image_size, image_size)).flatten()
np_img2=np.asarray(img2)
plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(np_img.reshape(image_size, image_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np_img2.reshape(image_size, image_size))
plt.axis('off')
plt.title("Cat and Dogs in GrayScale")
Output for first one
ValueError Traceback (most recent call last)
<ipython-input-47-0984b7467972> in <module>
28 plt.figure(figsize=(10,10))
29 plt.subplot(1, 2, 1)
---> 30 plt.imshow(np_img.reshape(img_size, img_size))
31 plt.axis('off')
32 plt.subplot(1, 2, 2)
ValueError: cannot reshape array of size 76964 into shape (300,300)
Output for second one
error Traceback (most recent call last)
<ipython-input-67-99d190c6fd41> in <module>
4 path = os.path.join(train_dog, image)
5 img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
----> 6 img = cv2.resize(img, (image_size, image_size)).flatten()
7 np_img=np.asarray(img)
8
error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
As I mentioned above, I tried to make a convertion from RGB to Grayscale, and make the images' size 1 type(128x128 optionally). I believed there is no mistake to convertion but resize is a problem that I can't solve for 2-3 days, eventhough I made tons of researches. I'm not sure but it could be a simple problem, like a said, I'm a beginner for now.
Note: I added 2 way of codes because there are the codes I understood best. I hope, you can help me, or teach me a new way, thank you in advance :)
#Appendixes:
Cat and Dog Dataset: https://www.kaggle.com/tongpython/cat-and-dog
The resizes and grayscale codes I've tried to implement are taken from: (In[5])