Skip to content

Instantly share code, notes, and snippets.

@hzitoun
Last active May 18, 2020 12:58
Show Gist options
  • Save hzitoun/af4c0b22f0a8a3c6b3934ed7cdbc90a9 to your computer and use it in GitHub Desktop.
Save hzitoun/af4c0b22f0a8a3c6b3934ed7cdbc90a9 to your computer and use it in GitHub Desktop.
Code for my medium story Serve a Deep Learning Image Classification Model Written in TensorFlow 2 as a REST API and Dockerize it! (with GPU support)
def read_image(image_path, image_size):
""" read an image using opencv """
image = cv2.imread(image_path) #BGR
# convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# resize using cubic interpolation, a way to deal with missing data (upsize or downsize)
image = cv2.resize(image, image_size, cv2.INTER_CUBIC)
return image
@hzitoun
Copy link
Author

hzitoun commented May 17, 2020

def load_dataset(lables_file_path, dataset_path, image_size):
    """ load data """
    
    with open(lables_file_path,  "r") as f:
        labels = f.read().splitlines() # ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
        
    images = []
    targets = []

    for image_file_name in tqdm(os.listdir(dataset_path), desc="loading images", unit="image loading"):
        
        try:
            image_path = os.path.join(dataset_path, image_file_name)
            images.append(image_loader(image_path, image_size))
            
            for idx in range(len(labels)):
                if labels[idx] in image_file_name: #image file name contains its label
                    targets.append(idx)
        except Exception as e:
            pass
       
    return np.array(images), np.array(targets)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment