[split] Kera Getting errors when following code example Image classification from scratch - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [split] Kera Getting errors when following code example Image classification from scratch (/thread-27613.html) |
[split] Kera Getting errors when following code example Image classification from scratch - hobbyist - Jun-13-2020 From here: https://keras.io/examples/vision/image_classification_from_scratch/ when running: import matplotlib.pyplot as plt plt.figure(figsize=(10, 10)) for images, labels in train_ds.take(1): for i in range(9): ax = plt.subplot(3, 3, i + 1) plt.imshow(images[i].numpy().astype("uint8")) plt.title(int(labels[i])) plt.axis("off")I got this error: Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9I found this post: https://github.com/tensorflow/models/issues/2194 and I ran this code: import io import os import sys import tensorflow as tf import PIL def main(argv): path_images = './images' filenames_src = tf.gfile.ListDirectory(path_images) for filename_src in filenames_src: stem, extension = os.path.splitext(filename_src) if (extension.lower() != '.jpg'): continue pathname_jpg = '{}/{}'.format(path_images, filename_src) with tf.gfile.GFile(pathname_jpg, 'rb') as fid: encoded_jpg = fid.read(4) # png if(encoded_jpg[0] == 0x89 and encoded_jpg[1] == 0x50 and encoded_jpg[2] == 0x4e and encoded_jpg[3] == 0x47): # copy jpg->png then encode png->jpg print('png:{}'.format(filename_src)) pathname_png = '{}/{}.png'.format(path_images, stem) tf.gfile.Copy(pathname_jpg, pathname_png, True) PIL.Image.open(pathname_png).convert('RGB').save(pathname_jpg, "jpeg") # gif elif(encoded_jpg[0] == 0x47 and encoded_jpg[1] == 0x49 and encoded_jpg[2] == 0x46): # copy jpg->gif then encode gif->jpg print('gif:{}'.format(filename_src)) pathname_gif = '{}/{}.gif'.format(path_images, stem) tf.gfile.Copy(pathname_jpg, pathname_gif, True) PIL.Image.open(pathname_gif).convert('RGB').save(pathname_jpg, "jpeg") elif(filename_src == 'beagle_116.jpg' or filename_src == 'chihuahua_121.jpg'): # copy jpg->jpeg then encode jpeg->jpg print('jpeg:{}'.format(filename_src)) pathname_jpeg = '{}/{}.jpeg'.format(path_images, stem) tf.gfile.Copy(pathname_jpg, pathname_jpeg, True) PIL.Image.open(pathname_jpeg).convert('RGB').save(pathname_jpg, "jpeg") elif(encoded_jpg[0] != 0xff or encoded_jpg[1] != 0xd8 or encoded_jpg[2] != 0xff): print('not jpg:{}'.format(filename_src)) if __name__ == "__main__": sys.exit(int(main(sys.argv) or 0))I edited: tf.gfileto tf.io.gfileand now I got this error: Traceback (most recent call last): File "source_code.py", line 90, in <module> sys.exit(int(main(sys.argv) or 0)) File "source_code.py", line 58, in main filenames_src = tf.io.gfile.ListDirectory(path_images) AttributeError: module 'tensorflow._api.v2.io.gfile' has no attribute 'ListDirectory'Any idea what should I do? RE: [split] Kera Getting errors when following code example Image classification from ... - hobbyist - Jun-13-2020 Ok, I have found a solution. Any idea how I access the dataset that I have downloaded on Colab, with the command that the tutorial that I have posted, indicates: !curl -O https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_3367a.zip !unzip -q kagglecatsanddogs_3367a.zip !ls !ls PetImages??? RE: [split] Kera Getting errors when following code example Image classification from ... - hobbyist - Jun-14-2020 I am trying to solve this error all morning: TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string.I am reading posts, I tried them, but without any success... Does anyone know what to do? RE: [split] Kera Getting errors when following code example Image classification from ... - amirian - Apr-13-2021 What was the solution? I have the same problem with train_ds but not with the val_ds. However no validation image is shown by running the code. (Jun-13-2020, 07:26 PM)hobbyist Wrote: Ok, I have found a solution. Any idea how I access the dataset that I have downloaded on Colab, with the command that the tutorial that I have posted, indicates: |