Creating and Deploying a Cat-Dog Image Classifier using TensorFlow and Streamlit- Part 1



Original Source Here

Data Ingestion and Manipulation

IMG_WIDTH=200
IMG_HEIGHT=200
img_folder='/kaggle/input/cat-and-dog/training_set/training_set/'

def create_dataset(img_folder):

img_data_array=[]
class_name=[]

for dir1 in os.listdir(img_folder):
print("Collecting images for: ",dir1)
for file in os.listdir(os.path.join(img_folder, dir1)):

image_path= os.path.join(img_folder, dir1, file)
image= cv2.imread( image_path, cv2.COLOR_BGR2RGB)
try:
image=cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH),interpolation = cv2.INTER_AREA)
except:
break
image=np.array(image)
image = image.astype('float32')
image /= 255
img_data_array.append(image)
class_name.append(dir1)
return img_data_array, class_name
# extract the image array and class name
img_data, class_name =create_dataset('/kaggle/input/cat-and-dog/training_set/training_set/')

A lot of things to cover here. Let’s get started with the basics: we need to ensure that the image is processed as data, every image has the same height and width, and the image quality is not forsaken in this resizing process. Finally, we need to collate those images(collection of pixel data, duh!) into one large array for our Neural Nets to work on!

I will cover some of the things which might fetch your attention here-

CV- OpenCV; an open source library for computer vision operations and algorithmscv2.imread-This method loads an image from a destinationcv2.COLOR_BGR2RGB- OpenCV reads images in BGR (Blue Green Red) colorspace ordering and we have to convert it back to RGB (Red Green Blue)cv2.INTER_AREA- This method resizes the image. We need to ensure that the aspect ratio is maintained while image manipulations. Ene quick thumbrule to follow is this-
If you are enlarging the image, you should prefer to use INTER_LINEAR or INTER_CUBIC interpolation. If you are shrinking the image, you should prefer to use INTER_AREA interpolation.
image.astype('float32') - For all weights and neuron activations, if you are using a method based on backpropagation for training updates, then you need a data type that approximates real numbers, so that you can apply fractional updates based on differentiation. Best weight values are often going to be fractional, non-whole numbers. Non-linearities such as sigmoid are also going to output floats. So after the input layer you have matrices of float values anyway. Hence, We need to ensure here that the tensors are read as floats valuesimage /= 255 - normalizing our input so that images are scaled between 0 and 1 (max pixel value is 255).

Once, I had the arrays prepared for cats and dogs, It was time to prepare the output variable as 0 and 1. I choose 1 for dogs and 0 for cats (I just prefer dogs, that’s all)

def dog_cat_mapping(a):
if a=="dogs":
return 1
else:return 0
class_name=list(map(dog_cat_mapping,class_name))
class_name=np.array(class_name)

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: