ValueError: invalid literal for int() with base 10: “image_path” :image_preloader of “tflearn” package

1.1k Views Asked by At

I'm trying to classify images of Cats & Dogs using following link.

In ANACONDA NAVIGATOR, i've faced problem to install package "tflearn" under Environment part. There have no any tflearn package. After suffering some problem through ANACONDA PROMPT, tflearn successfully installed in Anaconda3 folder. But then by running following code:

1st cell:

import numpy as np
import matplotlib.pyplot as plt 
import tensorflow as tf
import tflearn
from PIL import Image
%matplotlib inline
#for writing text files
import glob
import os     
import random 
#reading images from a text file
from tflearn.data_utils import image_preloader
import math

following WARNING has occured:

curses is not supported on this machine (please install/reinstall curses for an optimal experience

However after running following 3 cells i've not get any kind of error:

2nd cell (just for importing required image folder & creating data set as .txt format):

IMAGE_FOLDER = '/Users/sarfaraz/Anaconda3/Dataset_for_Cats_Dogs/train'
TRAIN_DATA = '/Users/sarfaraz/Anaconda3/Dataset_for_Cats_Dogs/training_data.txt'
TEST_DATA = '/Users/sarfaraz/Anaconda3/Dataset_for_Cats_Dogs/test_data.txt'
VALIDATION_DATA = '/Users/sarfaraz/Anaconda3/Dataset_for_Cats_Dogs/validation_data.txt'
train_proportion=0.7
test_proportion=0.2 
validation_proportion=0.1

3rd cell:

#read the image directories
filenames_image = os.listdir(IMAGE_FOLDER)
#shuffling the data is important otherwise the model will be fed with a single class data for a long time and 
#network will not learn properly
random.shuffle(filenames_image)

4th cell:

#total number of images
total=len(filenames_image)
##  *****training data******** 
fr = open(TRAIN_DATA, 'w')
train_files=filenames_image[0: int(train_proportion*total)]
for filename in train_files:
    if filename[0:3] == 'cat':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:3] == 'dog':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')

fr.close()
##  *****testing data******** 
fr = open(TEST_DATA, 'w')
test_files=filenames_image[int(math.ceil(train_proportion*total)):int(math.ceil((train_proportion+test_proportion)*total))]
for filename in test_files:
    if filename[0:3] == 'cat':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:3] == 'dog':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
fr.close()

##  *****validation data******** 
fr = open(VALIDATION_DATA, 'w')
valid_files=filenames_image[int(math.ceil((train_proportion+test_proportion)*total)):total]
for filename in valid_files:
    if filename[0:3] == 'cat':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:3] == 'dog':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
fr.close()

But after running following 5th cell(of code):

#Importing data
X_train, Y_train = image_preloader(TRAIN_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)

I have got an error as follows:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-f3e21e2c3936> in <module>()
      1 #Importing data
----> 2 X_train, Y_train = image_preloader(TRAIN_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)

~\Anaconda3\lib\site-packages\tflearn-0.3.2-py3.6.egg\tflearn\data_utils.py in image_preloader(target_path, image_shape, mode, normalize, grayscale, categorical_labels, files_extension, filter_channel)
    537                             continue
    538                     images.append(l[0])
--> 539                     labels.append(int(l[1]))
    540 
    541     n_classes = np.max(labels) + 1

ValueError: invalid literal for int() with base 10: 'sarfaraz/Anaconda3/Dataset_for_Cats_Dogs/train/cat.103.jpg'

Now my questions are:

Have there any Hardware/Software issue ? or the problem is in the code ? If error based on Code then how Vikramnk successfully run these codes (record from github) ?

Some Conditions:

All .txt files (as data set) have successfully created. My Machine: HP 245 g5 laptop with 4GB RAM

Hope I've explained my problem as good as possible. I believe at least one person will concentrate on my problem from "STACKOVERFLOW" because near to me Stackoverflow is only one source where i can solve my problem of coding within little time. THANK YOU for your patience!!

1

There are 1 best solutions below

0
Rakibul Hasan Roni On

The problem occurs when the conversion of String to int is not possible. In this particular case, the file you are converting "training_data.txt" has some symbols which cannot be converted to int. Typical cases are a space or some other symbols. I believe Your problem is the "_" in your folder name. If not, try finding any other spaces or unexpeted symbols in the file.

For example:

/Users/Awal/Desktop/Rakib/CNN/cats And Dogs/train/cat.1236.jpg 0

This will trigger an arror because the program finds the first space and takes the next integer and converts it to int. In this case "0"

Solution:

/Users/Awal/Desktop/Rakib/CNN/cats And Dogs/train/cat.1236.jpg 0


Try changing the Folder name "Dataset_for_Cats_Dogs" To a simple string like "DatastForCatsDogs".