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
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 368 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  [split] Explain the python code in this definition Led_Zeppelin 1 736 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 721 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  Correct the algorithm of image filter code saoko 6 1,998 May-08-2022, 05:06 PM
Last Post: saoko
  How to plot confusion matrix of multiclass classification Vaishali 0 1,350 Feb-10-2022, 02:34 PM
Last Post: Vaishali
  My code won't say Player wins or computer wins. No errors. its_a_meLuigi64 2 1,620 Dec-01-2021, 04:43 PM
Last Post: its_a_meLuigi64
  code decode, string, image ... teckow 2 2,046 Aug-20-2021, 07:02 PM
Last Post: teckow
  Rmarkdown opened by python code - errors Rav013 0 2,085 Apr-27-2021, 03:13 PM
Last Post: Rav013
Big Grin New to python, starting from scratch Pythnoobvent1971 1 2,007 Apr-16-2021, 01:33 PM
Last Post: Yoriz
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,113 Dec-06-2020, 10:10 PM
Last Post: Aizou

Forum Jump:

User Panel Messages

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