0

Source code link: https://github.com/rndbrtrnd/udacity-deep-learning/blob/master/1_notmnist.ipynb

I am working with some of the existing code already written to read image's data as part of deep-learning course using ndimage.imread which is depricated and I changed it to imageio.imread to proceed. But I am still facing some issues (I think it is related to bad image's data).

Initial Code to read image data using ndimage.imread (depreciated). This code does not work as ndimage is depricated from 1.2.0 version of SciPy.

try:
  image_data = (ndimage.imread(image_file).astype(float) - 
                pixel_depth / 2) / pixel_depth
  if image_data.shape != (image_size, image_size):
    raise Exception('Unexpected image shape: %s' % str(image_data.shape))
  dataset[image_index, :, :] = image_data
  image_index += 1
except IOError as e:
  print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')

I modified it to use imageio.imread

try:
        # read the image and normalize it
        image_data = (imageio.imread(image_file).astype(float) - pixel_depth/2)/pixel_depth
        if image_data.shape!= (image_size,image_size):
            raise Exception('Unexpected images shape: %s' % str(image_data.shape))
        dataset[image_index,:,:] = image_data
        image_index+=1
    except IOError as e:
        print('could not read the',image_file ,':',e,'hence skipping it.')

Issues description: While using imageio.imread, it is throwing the below error:

ValueError: Could not find a format to read the specified file in single-image mode
Kasid Khan
  • 639
  • 2
  • 8
  • 23

1 Answers1

-1

I spent a whole day to figure out a way to handle it and it turns out the solution is simpler than I thought.

Solution: I was able to solve this issue by catching the ValueError exception and continue with the reading of image files.

Here is the code:

image_size = 28
pixel_depth = 255.0
def load_letter(folder,min_num_images):
# read all the images name from the folder
image_files = os.listdir(folder)
# create a 3D data array
dataset = np.ndarray(shape=. 
   (len(image_files),image_size,image_size),dtype=np.float32)
image_index = 0
print(folder)
# read images from name from folder
for image in image_files:
    # create the path to the file
    image_file = os.path.join(folder,image)
    try:
        # read the image and normalize it
        image_data = (imageio.imread(image_file).astype(float) - pixel_depth/2)/pixel_depth
        if image_data.shape!= (image_size,image_size):
            raise Exception('Unexpected images shape: %s' % str(image_data.shape))
        # store it in 3D data set
        dataset[image_index,:,:] = image_data
        image_index+=1
    except (IOError,ValueError) as e:
        print('could not read the',image_file ,':',e,'hence skipping it.')

Output: The bad files name where printed on the output and reading of files continued.

Pickling notMNIST_large/A.pickle.
notMNIST_large/A
could not read the notMNIST_large/A/RnJlaWdodERpc3BCb29rSXRhbGljLnR0Zg==.png : 
Could not find a format to read the specified file in single-image mode hence 
skipping it.
could not read the notMNIST_large/A/Um9tYW5hIEJvbGQucGZi.png : 
Could not find a format to read the specified file in single-image mode hence skipping it.
could not read the notMNIST_large/A/SG90IE11c3RhcmQgQlROIFBvc3Rlci50dGY=.png : 
Could not find a format to read the specified file in single-image mode hence skipping it.
Kasid Khan
  • 639
  • 2
  • 8
  • 23