Python Forum
[split] Kera Getting errors when following code example Image classification from scratch
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Kera Getting errors when following code example Image classification from scratch
#1
From here:

https://keras.io/examples/vision/image_c...m_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 0xd9
I 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.gfile
to
 tf.io.gfile 
and 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?
Reply
#2
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
???
Reply
#3
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?
Reply
#4
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:

!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
???
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code decode, string, image ... teckow 2 262 Aug-20-2021, 07:02 PM
Last Post: teckow
  Rmarkdown opened by python code - errors Rav013 0 494 Apr-27-2021, 03:13 PM
Last Post: Rav013
Big Grin New to python, starting from scratch Pythnoobvent1971 1 538 Apr-16-2021, 01:33 PM
Last Post: Yoriz
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 779 Dec-06-2020, 10:10 PM
Last Post: Aizou
  [split] Image recognition technique. SrinavasRao 3 680 Sep-22-2020, 07:32 AM
Last Post: hussainmujtaba
  Applying Multi-Class Classification instead of binary classification alex80 1 572 Sep-18-2020, 07:39 AM
Last Post: alex80
  [split] Python code help sairam17519 3 886 Sep-10-2020, 01:44 PM
Last Post: sairam17519
  How to Split Output Audio on Text to Speech Code Base12 2 3,682 Aug-29-2020, 03:23 AM
Last Post: Base12
  How to split and combine embedded lines using less code pjfarley3 6 844 Aug-13-2020, 09:13 PM
Last Post: pjfarley3
  [split] SyntaxError when trying to execute code on Windows nehaya 2 699 Aug-04-2020, 11:18 AM
Last Post: nehaya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020