|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# In[1]: |
| 5 | + |
| 6 | + |
| 7 | +import os |
| 8 | +import random |
| 9 | +from shutil import copyfile |
| 10 | + |
| 11 | + |
| 12 | +# In[2]: |
| 13 | + |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | + |
| 19 | +# In[3]: |
| 20 | + |
| 21 | + |
| 22 | +import matplotlib.pyplot as plt |
| 23 | +import seaborn as sns |
| 24 | +from matplotlib.image import imread |
| 25 | +import pathlib |
| 26 | + |
| 27 | + |
| 28 | +# In[4]: |
| 29 | + |
| 30 | + |
| 31 | +image_folder = ['cloudy', 'foggy', 'rainy', 'shine', 'sunrise'] |
| 32 | +nimgs = {} |
| 33 | +for i in image_folder: |
| 34 | + nimages = len(os.listdir('../dataset/'+i+'/')) |
| 35 | + nimgs[i]=nimages |
| 36 | +plt.figure(figsize=(10, 8)) |
| 37 | +plt.bar(range(len(nimgs)), list(nimgs.values()), align='center') |
| 38 | +plt.xticks(range(len(nimgs)), list(nimgs.keys())) |
| 39 | +plt.title('Distribution of different classes of Dataset') |
| 40 | +plt.show() |
| 41 | + |
| 42 | + |
| 43 | +# In[5]: |
| 44 | + |
| 45 | + |
| 46 | +image_folder = ['cloudy', 'foggy', 'rainy', 'shine', 'sunrise'] |
| 47 | + |
| 48 | +for i in image_folder: |
| 49 | + sample_images = list(pathlib.Path('../dataset/'+i+'/').rglob('*/')) |
| 50 | + np.random.seed(42) |
| 51 | + rand_imgs = np.random.choice(sample_images, size=10*10) |
| 52 | + |
| 53 | + shapes = [] |
| 54 | + for img in rand_imgs: |
| 55 | + shapes.append(imread(str(img)).shape) |
| 56 | + |
| 57 | + shapes = pd.DataFrame().assign(X=pd.Series(shapes).map(lambda s: s[0]), Y=pd.Series(shapes).map(lambda s: s[1])) |
| 58 | + |
| 59 | + plt.figure(figsize=(12, 8)) |
| 60 | + sns.set_context("notebook", font_scale=1.5) |
| 61 | + sns.kdeplot(shapes['X'], bw=75) |
| 62 | + sns.kdeplot(shapes['Y'], bw=75) |
| 63 | + plt.title('Distribution of {}_image Sizes'.format(i)) |
| 64 | + ax = plt.gca() |
| 65 | + ax.set_xlim(0, ax.get_xlim()[1]) |
| 66 | + |
| 67 | + |
| 68 | +# In[ ]: |
| 69 | + |
| 70 | + |
| 71 | +try: |
| 72 | + os.mkdir('../weather_pred/Data') |
| 73 | + os.mkdir('../weather_pred/Data/training') |
| 74 | + os.mkdir('..weather_pred/Data/validation') |
| 75 | + os.mkdir('../weather_pred/Data/training/cloudy') |
| 76 | + os.mkdir('../weather_pred/Data/training/foggy') |
| 77 | + os.mkdir('../weather_pred/Data/training/rainy') |
| 78 | + os.mkdir('../weather_pred/Data/training/shine') |
| 79 | + os.mkdir('../weather_pred/Data/training/sunrise') |
| 80 | + os.mkdir('../weather_pred/Data/validation/cloudy') |
| 81 | + os.mkdir('../weather_pred/Data/validation/foggy') |
| 82 | + os.mkdir('../weather_pred/Data/validation/rainy') |
| 83 | + os.mkdir('../weather_pred/Data/validation/shine') |
| 84 | + os.mkdir('../weather_pred/Data/validation/sunrise') |
| 85 | +except OSError: |
| 86 | + pass |
| 87 | + |
| 88 | + |
| 89 | +# In[ ]: |
| 90 | + |
| 91 | + |
| 92 | +def split_data(SOURCE, TRAINING, VALIDATION, SPLIT_SIZE): |
| 93 | + files = [] |
| 94 | + for filename in os.listdir(SOURCE): |
| 95 | + file = SOURCE + filename |
| 96 | + if os.path.getsize(file) > 0: |
| 97 | + files.append(filename) |
| 98 | + else: |
| 99 | + print(filename + " is zero length, so ignoring.") |
| 100 | + |
| 101 | + training_length = int(len(files) * SPLIT_SIZE) |
| 102 | + valid_length = int(len(files) - training_length) |
| 103 | + shuffled_set = random.sample(files, len(files)) |
| 104 | + training_set = shuffled_set[0:training_length] |
| 105 | + valid_set = shuffled_set[training_length:] |
| 106 | + |
| 107 | + for filename in training_set: |
| 108 | + this_file = SOURCE + filename |
| 109 | + destination = TRAINING + filename |
| 110 | + copyfile(this_file, destination) |
| 111 | + |
| 112 | + for filename in valid_set: |
| 113 | + this_file = SOURCE + filename |
| 114 | + destination = VALIDATION + filename |
| 115 | + copyfile(this_file, destination) |
| 116 | + |
| 117 | + |
| 118 | +# In[ ]: |
| 119 | + |
| 120 | + |
| 121 | +CLOUDY_SOURCE_DIR = '.../dataset/cloudy/' |
| 122 | +TRAINING_CLOUDY_DIR = '.../weather_pred/Data/training/cloudy/' |
| 123 | +VALID_CLOUDY_DIR = '.../weather_pred/Data/validation/cloudy/' |
| 124 | + |
| 125 | +FOGGY_SOURCE_DIR = '.../dataset/foggy/' |
| 126 | +TRAINING_FOGGY_DIR = '../weather_pred/Data/training/foggy/' |
| 127 | +VALID_FOGGY_DIR = '.../weather_pred/Data/validation/foggy/' |
| 128 | + |
| 129 | +RAINY_SOURCE_DIR = '.../dataset/rainy/' |
| 130 | +TRAINING_RAINY_DIR = '.../weather_pred/Data/training/rainy/' |
| 131 | +VALID_RAINY_DIR = '.../weather_pred/Data/validation/rainy/' |
| 132 | + |
| 133 | +SHINE_SOURCE_DIR = '.../dataset/shine/' |
| 134 | +TRAINING_SHINE_DIR = '.../weather_pred/Data/training/shine/' |
| 135 | +VALID_SHINE_DIR = '.../weather_pred/Data/validation/shine/' |
| 136 | + |
| 137 | +SUNRISE_SOURCE_DIR = '.../dataset/sunrise/' |
| 138 | +TRAINING_SUNRISE_DIR = '.../weather_pred/Data/training/sunrise/' |
| 139 | +VALID_SUNRISE_DIR = '.../weather_pred/Data/validation/sunrise/' |
| 140 | + |
| 141 | + |
| 142 | +# In[ ]: |
| 143 | + |
| 144 | + |
| 145 | +split_size = .85 |
| 146 | + |
| 147 | + |
| 148 | +# In[ ]: |
| 149 | + |
| 150 | + |
| 151 | +split_data(CLOUDY_SOURCE_DIR, TRAINING_CLOUDY_DIR, VALID_CLOUDY_DIR, split_size) |
| 152 | +split_data(FOGGY_SOURCE_DIR, TRAINING_FOGGY_DIR, VALID_FOGGY_DIR, split_size) |
| 153 | +split_data(RAINY_SOURCE_DIR, TRAINING_RAINY_DIR, VALID_RAINY_DIR, split_size) |
| 154 | +split_data(SHINE_SOURCE_DIR, TRAINING_SHINE_DIR, VALID_SHINE_DIR, split_size) |
| 155 | +split_data(SUNRISE_SOURCE_DIR, TRAINING_SUNRISE_DIR, VALID_SUNRISE_DIR, split_size) |
| 156 | + |
| 157 | + |
| 158 | +# In[6]: |
| 159 | + |
| 160 | + |
| 161 | +image_folder = ['cloudy', 'foggy', 'rainy', 'shine', 'sunrise'] |
| 162 | +nimgs = {} |
| 163 | +for i in image_folder: |
| 164 | + nimages = len(os.listdir('../weather_pred/Data/training/'+i+'/')) |
| 165 | + nimgs[i]=nimages |
| 166 | +plt.figure(figsize=(9, 6)) |
| 167 | +plt.bar(range(len(nimgs)), list(nimgs.values()), align='center') |
| 168 | +plt.xticks(range(len(nimgs)), list(nimgs.keys())) |
| 169 | +plt.title('Distribution of different classes in Training Dataset') |
| 170 | +plt.show() |
| 171 | + |
| 172 | + |
| 173 | +# In[7]: |
| 174 | + |
| 175 | + |
| 176 | +for i in ['cloudy', 'foggy', 'rainy', 'shine', 'sunrise']: |
| 177 | + print('Training {} images are: '.format(i)+str(len(os.listdir('../weather_pred/Data/training/'+i+'/')))) |
| 178 | + |
| 179 | + |
| 180 | +# In[8]: |
| 181 | + |
| 182 | + |
| 183 | +image_folder = ['cloudy', 'foggy', 'rainy', 'shine', 'sunrise'] |
| 184 | +nimgs = {} |
| 185 | +for i in image_folder: |
| 186 | + nimages = len(os.listdir('../weather_pred/Data/validation/'+i+'/')) |
| 187 | + nimgs[i]=nimages |
| 188 | +plt.figure(figsize=(9, 6)) |
| 189 | +plt.bar(range(len(nimgs)), list(nimgs.values()), align='center') |
| 190 | +plt.xticks(range(len(nimgs)), list(nimgs.keys())) |
| 191 | +plt.title('Distribution of different classes in Validation Dataset') |
| 192 | +plt.show() |
| 193 | + |
| 194 | + |
| 195 | +# In[9]: |
| 196 | + |
| 197 | + |
| 198 | +for i in ['cloudy', 'foggy', 'rainy', 'shine', 'sunrise']: |
| 199 | + print('Valid {} images are: '.format(i)+str(len(os.listdir('../weather_pred/Data/validation/'+i+'/')))) |
| 200 | + |
| 201 | + |
| 202 | +# In[ ]: |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
0 commit comments