plot images

This commit is contained in:
Maciej Sobkowiak 2022-02-16 19:22:34 +01:00
parent a4a53ca85e
commit e0753c0b4f
5 changed files with 66 additions and 90 deletions

View File

@ -2,6 +2,7 @@ import pandas as pd
FEATURES ='../data/train_features'
LABELS = '../data/train_labels'
JPG_IMAGES = '../images'
RGB_DIR = "rgb"
FC_DIR = "fc"
MASK_DIR = "mask"
METADATA = pd.read_csv('../data/train_metadata.csv')

View File

@ -1,41 +1,41 @@
train_datagen = ImageDataGenerator(rescale=1 / 255.0,
horizontal_flip=True,
vertical_flip=True,
validation_split=0.2)
from consts import JPG_IMAGES, RGB_DIR, MASK_DIR, FC_DIR, BATCH, IMG_SIZE
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# mask_datagen = ImageDataGenerator(rescale=1/255,
# validation_split=0.2)
def create_generators(mode='train'):
'''
mode can be train or validation.
'''
if(mode=='train'):
subset = 'training'
else:
subset = 'validation'
#training data
#rgb images
rgb_dir = '../input/ai4earth-mask-the-clouds/RGB_images'
rgb_gen = train_datagen.flow_from_directory(directory=rgb_dir,
target_size= img_size,
train_datagen = ImageDataGenerator(rescale=1 / 255.0,
horizontal_flip=True,
vertical_flip=True,
validation_split=0.2)
rgb_gen = train_datagen.flow_from_directory(directory=os.path.join(JPG_IMAGES, RGB_DIR),
target_size= IMG_SIZE,
batch_size=BATCH,
class_mode=None,
classes=None,
shuffle=False,
seed=seed,
subset='training')
subset=subset)
#false color (nir,green,blue)
fc_dir = '../input/ai4earth-mask-the-clouds/False_color'
fc_gen = train_datagen.flow_from_directory(
directory=fc_dir,
target_size= img_size,
mask_gen = train_datagen.flow_from_directory(
directory=os.path.join(JPG_IMAGES, MASK_DIR ),
target_size= IMG_SIZE,
batch_size=BATCH,
class_mode=None,
classes=None,
shuffle=False,seed=seed,
subset='training')
shuffle=False,
subset=subset)
#training labels
mask_dir ='../input/ai4earth-mask-the-clouds/Masks'
mask_gen = train_datagen.flow_from_directory(
directory=mask_dir,
target_size= img_size,
batch_size=BATCH,
class_mode=None,
classes=None,
shuffle=False,seed=seed,
subset='training')
# train_genenerator = zip(rgb_gen,mask_gen)
# for (imgs, mask) in train_genenerator:
# yield (imgs, mask)
return rgb_gen, mask_gen

View File

@ -69,5 +69,6 @@ def convert_tif_to_jpg(features, labels,
#konwersja maski
mask= rio.open(labels).read().reshape(512,512,1)
mask *= 255
cv2.imwrite(filename= mask_path + f'/{file_name}.jpeg',
img=mask.astype(np.uint8))

View File

@ -1,33 +1,28 @@
import os
import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
import time
#progess bar
import sys
import random
from tqdm import tqdm
import cv2
import warnings
#deep learning
from consts import SEED, JPG_IMAGES, FC_DIR, FEATURES, MASK_DIR, LABELS
from consts import SEED, JPG_IMAGES, FC_DIR, FEATURES, MASK_DIR, LABELS, RGB_DIR
from helpers import create_folder, convert_tif_to_jpg, progress_bar
warnings.filterwarnings('ignore')
def transform_photo(tif_dir):
dp = create_folder(tif_dir, JPG_IMAGES)
fc = create_folder(FC_DIR, dp)
mk = create_folder(MASK_DIR, dp)
convert_tif_to_jpg(os.path.join(FEATURES, tif_dir), os.path.join(LABELS, tif_dir + ".tif"), dp, fc, mk)
from utils import plot_image_grid
if __name__ == "__main__":
dirs = os.listdir(FEATURES)
progress_bar(0, len(dirs), prefix = 'Converting TIF to JPG:', suffix = 'Complete', length = 50)
for i, d in enumerate(dirs):
progress_bar(i, len(dirs), prefix = 'Converting TIF to JPG:', suffix = 'Complete', length = 50)
if d != ".DS_Store":
transform_photo(d)
dp = create_folder(RGB_DIR, JPG_IMAGES)
fc = create_folder(FC_DIR, JPG_IMAGES)
mk = create_folder(MASK_DIR, JPG_IMAGES)
if(len(sys.argv) <= 1):
progress_bar(0, len(dirs), prefix = 'Converting TIF to JPG:', suffix = 'Complete', length = 50)
for i, d in enumerate(dirs):
progress_bar(i, len(dirs), prefix = 'Converting TIF to JPG:', suffix = 'Complete', length = 50)
convert_tif_to_jpg(os.path.join(FEATURES, d), os.path.join(LABELS, d + ".tif"), dp, fc, mk)
elif(sys.argv[1] == '--show'):
img_names = [random.choice(os.listdir(dp)) for _ in range(3)]
plot_image_grid(img_names)

View File

@ -1,47 +1,26 @@
def plot_image_grid(image_list,
label_list,
sample_images=False,
num_images=6,
pre_title='class',
num_img_per_row=3,
cmap=None,
img_h_w=3):
'''viz images from a list of images and labels
INPUTS:
image_list: a list of images to be plotted,
label_list: a list of correspomding image labels'''
import matplotlib.pyplot as plt
import cv2
import os
from consts import JPG_IMAGES, RGB_DIR, MASK_DIR
def plot_image_grid(image_names):
#number of img rows
n_row= num_images//num_img_per_row
n_row= len(image_names)
plt.subplots(n_row,num_img_per_row,figsize=(img_h_w*num_img_per_row,img_h_w*n_row))
plt.subplots(n_row, 2, figsize=(6, 3*n_row))
if sample_images:
#select_random images
sampled_ids = random.choices(np.arange(0,len(image_list)),k=num_images)
for i,idx in enumerate(sampled_ids):
img = image_list[idx]
label = label_list[i]
plt.subplot(n_row,num_img_per_row,i+1)
plt.title(f'{pre_title} - {label}')
plt.axis('off')
plt.imshow(img,cmap=cmap)
else:
for i,img in enumerate(image_list):
label = label_list[i]
plt.subplot(n_row,num_img_per_row,i+1)
plt.title(f'{pre_title} - {label}')
plt.axis('off')
plt.imshow(img,cmap=cmap)
# break the loop
if i==num_images-1 :
break
for i, img in enumerate(image_names):
r_img = cv2.imread(os.path.join(JPG_IMAGES, RGB_DIR, img))
plt.subplot(n_row, 2, i*2+1)
plt.title(f'RGB - {img}')
plt.axis('off')
plt.imshow(r_img, cmap=None)
m_img = cv2.imread(os.path.join(JPG_IMAGES, MASK_DIR, img))
plt.subplot(n_row, 2, i*2+2)
plt.title(f'MASK - {img}')
plt.axis('off')
plt.imshow(m_img, cmap=None)
#show
plt.tight_layout()