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' FEATURES ='../data/train_features'
LABELS = '../data/train_labels' LABELS = '../data/train_labels'
JPG_IMAGES = '../images' JPG_IMAGES = '../images'
RGB_DIR = "rgb"
FC_DIR = "fc" FC_DIR = "fc"
MASK_DIR = "mask" MASK_DIR = "mask"
METADATA = pd.read_csv('../data/train_metadata.csv') METADATA = pd.read_csv('../data/train_metadata.csv')

View File

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

View File

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

View File

@ -1,33 +1,28 @@
import os import os
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import random
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import time import time
import sys
#progess bar import random
from tqdm import tqdm from tqdm import tqdm
import cv2 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 from helpers import create_folder, convert_tif_to_jpg, progress_bar
from utils import plot_image_grid
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)
if __name__ == "__main__": if __name__ == "__main__":
dirs = os.listdir(FEATURES) dirs = os.listdir(FEATURES)
progress_bar(0, len(dirs), prefix = 'Converting TIF to JPG:', suffix = 'Complete', length = 50) dp = create_folder(RGB_DIR, JPG_IMAGES)
for i, d in enumerate(dirs): fc = create_folder(FC_DIR, JPG_IMAGES)
progress_bar(i, len(dirs), prefix = 'Converting TIF to JPG:', suffix = 'Complete', length = 50) mk = create_folder(MASK_DIR, JPG_IMAGES)
if d != ".DS_Store": if(len(sys.argv) <= 1):
transform_photo(d) 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, import matplotlib.pyplot as plt
label_list, import cv2
sample_images=False, import os
num_images=6, from consts import JPG_IMAGES, RGB_DIR, MASK_DIR
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'''
def plot_image_grid(image_names):
#number of img rows #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: for i, img in enumerate(image_names):
#select_random images r_img = cv2.imread(os.path.join(JPG_IMAGES, RGB_DIR, img))
sampled_ids = random.choices(np.arange(0,len(image_list)),k=num_images) plt.subplot(n_row, 2, i*2+1)
plt.title(f'RGB - {img}')
for i,idx in enumerate(sampled_ids): plt.axis('off')
plt.imshow(r_img, cmap=None)
img = image_list[idx] m_img = cv2.imread(os.path.join(JPG_IMAGES, MASK_DIR, img))
label = label_list[i] plt.subplot(n_row, 2, i*2+2)
plt.subplot(n_row,num_img_per_row,i+1) plt.title(f'MASK - {img}')
plt.title(f'{pre_title} - {label}') plt.axis('off')
plt.axis('off') plt.imshow(m_img, cmap=None)
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
#show #show
plt.tight_layout() plt.tight_layout()