Home > OS >  Having issues assigning labels to image data
Having issues assigning labels to image data

Time:12-29

``The directory that the data is in only has 50 files but after assigning images labels and resizing when i print the len of data i get 750. I am starting to wonder if the arrays within the images are being separated and assigned their own label.code where images are given a label and resized

import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import random
Directory='../input/hotdog'
Directory1='../input/nothotdog'
image_size=100
data=[]
for directory in Directory:
    folder=os.path.join(Directory)
    label=0 
    for img in os.listdir(Directory):
        img_path=os.path.join(Directory, img)
        img_arr= cv2.imread(img_path)
        img_arr= cv2.resize(img_arr, dsize=(image_size, image_size))
        data.append([img_arr, label])
print(len(data))
for directory in Directory1:
    folder=os.path.join(Directory1)
    label=1 
    for img in os.listdir(Directory1):
        img_path=os.path.join(Directory1, img)
        img_arr= cv2.imread(img_path)
        img_arr= cv2.resize(img_arr, dsize=(image_size, image_size))
        data.append([img_arr, label])

CodePudding user response:

Your line

for directory in Directory:

loops over the charactes in Directory which is '../input/hotdog'.

You should remove the for loop alltogether:

import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import random
Directory='../input/hotdog'
Directory1='../input/nothotdog'
image_size=100
data=[]

label=0
for img in os.listdir(Directory):
    img_path=os.path.join(Directory, img)
    img_arr= cv2.imread(img_path)
    img_arr= cv2.resize(img_arr, dsize=(image_size, image_size))
    data.append([img_arr, label])

print(len(data))

label=1
for img in os.listdir(Directory1):
    img_path=os.path.join(Directory1, img)
    img_arr= cv2.imread(img_path)
    img_arr= cv2.resize(img_arr, dsize=(image_size, image_size))
    data.append([img_arr, label])
  • Related