# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/BRIJNANDA1979/CNN-Sentinel/blob/master/CNN_model_for_21_classes_of_UC_Data_Read_and_Convert_tiff_to_jpeg_version_2.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + colab={"base_uri": "https://localhost:8080/"} id="AjiPEaxDWHyi" outputId="ac5b2459-4b1f-44ba-9244-b6818b944e82"
#https://stackoverflow.com/questions/50207292/how-to-convert-geotiff-to-jpg-in-python-or-java
from osgeo import gdal
    
options_list = [
    '-ot Byte',
    '-of JPEG',
    '-b 1 2 3',
    '-scale'
]           

options_string = " ".join(options_list)
    
gdal.Translate(
    '/content/drive/MyDrive/Data/RGB.jpg',
    '/content/drive/MyDrive/Big/S2A_MSIL2A_20170613T101031_0_55/RGB.tif',
    options=options_string
)


# + colab={"base_uri": "https://localhost:8080/"} id="DzEKLRD9wN6C" outputId="9b421c24-86bd-4bc2-d060-6d24bb7598d3"
import os
from PIL import Image
os.chdir('/content/drive/MyDrive/Data')
path = os.getcwd()
print(path)
im = Image.open('/content/drive/MyDrive/Data/a.tiff')
im.convert("RGB").save('/content/drive/MyDrive/Data/out.jpeg', "JPEG", quality=100)

# + colab={"base_uri": "https://localhost:8080/"} id="3sPq_g8hszjt" outputId="9dced770-04f9-49b8-f922-be46401490f7"
#https://stackoverflow.com/questions/37747021/create-numpy-array-of-images
resolution = 64

import glob
import numpy as np
from PIL import Image

X_data = []
files = glob.glob(r"/content/drive/MyDrive/Data/*.jpg")
for my_file in files:
    print(my_file)
    
    image = Image.open(my_file).convert('RGB')
    image = np.array(image)
    X_data.append(image)

print('X_data shape:', np.array(X_data).shape)

# + id="kh9fUILIqdje"
import io
from PIL import Image
from PIL import ImageCms

def convert_to_srgb(img):
    '''Convert PIL image to sRGB color space (if possible)'''
    icc = img.info.get('icc_profile', '')
    if icc:
        io_handle = io.BytesIO(icc)     # virtual file
        src_profile = ImageCms.ImageCmsProfile(io_handle)
        dst_profile = ImageCms.createProfile('sRGB')
        img = ImageCms.profileToProfile(img, src_profile, dst_profile)
    return img

img = Image.open('/content/drive/MyDrive/Data/RGB.jpg')
img_conv = convert_to_srgb(img)
if img.info.get('icc_profile', '') != img_conv.info.get('icc_profile', ''):
    # ICC profile was changed -> save converted file
    img_conv.save('/content/drive/MyDrive/Data/RGB.jpg',
                  format = 'JPEG',
                  quality = 100,
                  icc_profile = img_conv.info.get('icc_profile',''))

# + id="0jQ3zefoWFgC"
import cv2
import numpy as np

img = cv2.imread('/content/drive/MyDrive/Data/RGB.jpg')
res = cv2.resize(img, dsize=(64,64), interpolation=cv2.INTER_CUBIC)


# + colab={"base_uri": "https://localhost:8080/"} id="Wsz7VK8-sGoQ" outputId="81b1a2f8-31a8-4277-8ce0-5f33d935f475"
# !pip install rasterio

# + colab={"base_uri": "https://localhost:8080/", "height": 340} id="XPROntAsr4fl" outputId="0e5b55e7-a604-4dcb-c32b-e1def4ddd97f"
#21 classes 256x256 pixels
import rasterio
from rasterio.plot import show
img = r'/content/drive/MyDrive/UCMerced_LandUse/Images/agricultural/agricultural00.tif'
img = rasterio.open(fp)
show(img)

# + colab={"base_uri": "https://localhost:8080/"} id="sStpckXSsU79" outputId="37a1a8ac-a243-4a40-950e-a1f7e14564eb"
print(img.crs)

# + colab={"base_uri": "https://localhost:8080/"} id="WUmgf09Ysp6X" outputId="f1236444-0f95-425a-bfa0-de3544777125"
print(img.height, img.width)

# + colab={"base_uri": "https://localhost:8080/", "height": 286} id="0q7WaBJVtShq" outputId="4792e050-eeb5-4ed0-8557-18b254171559"
#for Reading particular band data of image
from osgeo import gdal
import matplotlib.pyplot as plt
image = gdal.Open('/content/drive/MyDrive/UCMerced_LandUse/Images/agricultural/agricultural00.tif', gdal.GA_ReadOnly) 
# Note GetRasterBand() takes band no. starting from 1 not 0
band = dataset.GetRasterBand(3)
arr = band.ReadAsArray()
plt.imshow(arr)

# + colab={"base_uri": "https://localhost:8080/"} id="nOQeoAILuB1E" outputId="fea6fa06-4e98-497b-91a0-9569bab26af0"
#number of bands
image.RasterCount

# + colab={"base_uri": "https://localhost:8080/"} id="UuD29n2WuJG-" outputId="3b194c1e-3483-4dd3-e492-824a44f1b264"
image.RasterXSize

# + colab={"base_uri": "https://localhost:8080/"} id="dZ3tkfpruPpM" outputId="26513015-ef58-4b6a-a216-c30936da2f82"
image.RasterYSize

# + colab={"base_uri": "https://localhost:8080/"} id="c0tgmnYjuSgA" outputId="25c6e305-a3d4-4ef7-b94b-7ab233ec0596"
img = image.GetRasterBand(1)
img.GetStatistics(True,True) # shows min, max,mean, s.d.

# + colab={"base_uri": "https://localhost:8080/"} id="vWkDcwtSu7Gy" outputId="d53d80f5-b69f-42b0-dd8c-91d732f68979"
img = image.GetRasterBand(2)
img.GetStatistics(True,True) # shows min, max,mean, s.d.

# + colab={"base_uri": "https://localhost:8080/"} id="E8LEUqfgvDYG" outputId="16a8c4ff-ef22-4c6b-de94-935980b33ee4"
img = image.GetRasterBand(3)
img.GetStatistics(True,True)# shows min, max,mean, s.d.

# + id="giTZF8gWwpqA"


# + [markdown] id="f9gKgAv5wuqt"
# ## Reading tif files and learning through CNN

# + id="bR8IKjaDw3O6"
import os   
path = '/content/drive/MyDrive/UCMerced_LandUse/Images'
os.chdir(path)


# + colab={"base_uri": "https://localhost:8080/"} id="01JMugdBxgQ-" outputId="51008a81-a55c-4829-83a9-3f2a529438fe"
x = os.listdir(path)
print(x)
print(len(x))

# + colab={"base_uri": "https://localhost:8080/"} id="F49wLAaCx_S3" outputId="1b53052a-61ca-4552-a574-cd7f4fe2e8fa"
#for Reading particular band data of image
from osgeo import gdal
import matplotlib.pyplot as plt
import os
start_path = '.' # current directory
for path,dirs,files in os.walk(start_path):
    print(files)
    for filename in files:
        s = os.path.join(path,filename)
        


# Note GetRasterBand() takes band no. starting from 1 not 0


# + colab={"base_uri": "https://localhost:8080/"} id="e2lOA7Ko1aJJ" outputId="99536a08-6894-455a-ceff-48324f0e25c0"
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# import the needed packages
import tensorflow as tf
from keras import losses 
from keras import optimizers 
from keras import metrics 
import matplotlib.pyplot as plt
import matplotlib.image as img
import tensorflow.keras as keras
from keras.preprocessing import image
import numpy as np
from keras.models import model_from_json

import os   

path = '/content/drive/MyDrive/UCMerced_LandUse/Images'
os.chdir(path)
list_of_dir = os.listdir(path)



# define and move to dataset directory
datasetdir = path
import os
os.chdir(datasetdir)


  
# shortcut to the ImageDataGenerator class
ImageDataGenerator = keras.preprocessing.image.ImageDataGenerator
gen = ImageDataGenerator()
iterator = gen.flow_from_directory(
    os.getcwd(), 
    target_size=(256,256), 
    classes=('harbor', 'river', 'mobilehomepark', 'golfcourse', 'agricultural', 'runway', 'buildings', 'forest', 'airplane', 'baseballdiamond', 'overpass', 'intersection', 'chaparral', 'sparseresidential', 'parkinglot', 'tenniscourt', 'mediumresidential', 'denseresidential', 'beach', 'freeway', 'storagetanks')
)
# we can guess that the iterator has a next function, 
# because all python iterators have one. 
batch = iterator.next()
print(len(batch))
print(type(batch[0]))
print(batch[0].shape)
print(batch[0].dtype)
#print(batch[0].max())
#print(batch[1].shape)
#print(batch[1].dtype)
#print(type(batch[1]))
#the first element is an array of 32 images with 256x256 pixels, and 3 color channels, encoded as floats in the range 0 to 255
#The second element contains the 32 corresponding labels.


# + colab={"base_uri": "https://localhost:8080/"} id="2WtSwXqD9i8U" outputId="92134010-785e-4093-af8d-433713dc3e25"
batch = iterator.next()
print(len(batch))
print(type(batch[1]))
print(batch[1].shape)
print(batch[1].dtype)

# + colab={"base_uri": "https://localhost:8080/"} id="Q9oqLY5t9lcs" outputId="94ca4336-0266-451b-9564-5e55926d84c9"
#Augmentation by Flipping images
#Now, let's make the transformation a bit more complex. This time, the ImageDataGenerator will flip, zoom, and rotate the images on a random basis   
imgdatagen = ImageDataGenerator(
    rescale = 1/255., 
    horizontal_flip = True, 
    zoom_range = 0.3, 
    rotation_range = 15.,
    validation_split = 0.1,
)
batch_size = 30
height, width = (256,256)
train_dataset = imgdatagen.flow_from_directory(
    os.getcwd(),
    target_size = (height, width), 
    classes=('harbor', 'river', 'mobilehomepark', 'golfcourse', 'agricultural', 'runway', 'buildings', 'forest', 'airplane', 'baseballdiamond', 'overpass', 'intersection', 'chaparral', 'sparseresidential', 'parkinglot', 'tenniscourt', 'mediumresidential', 'denseresidential', 'beach', 'freeway', 'storagetanks'),
    batch_size = batch_size,
    subset = 'training'
)

val_dataset = imgdatagen.flow_from_directory(
    os.getcwd(),
    target_size = (height, width), 
    classes=('harbor', 'river', 'mobilehomepark', 'golfcourse', 'agricultural', 'runway', 'buildings', 'forest', 'airplane', 'baseballdiamond', 'overpass', 'intersection', 'chaparral', 'sparseresidential', 'parkinglot', 'tenniscourt', 'mediumresidential', 'denseresidential', 'beach', 'freeway', 'storagetanks'),
    batch_size = batch_size,
    subset = 'validation'
)
model = keras.models.Sequential()

initializers = {
    
}
model.add( 
    keras.layers.Conv2D(
        24, 5, input_shape=(256,256,3), 
        activation='relu', 
    )
)
model.add( keras.layers.MaxPooling2D(2) )
model.add( 
    keras.layers.Conv2D(
        48, 5, activation='relu', 
    )
)
model.add( keras.layers.MaxPooling2D(2) )
model.add( 
    keras.layers.Conv2D(
        96, 5, activation='relu', 
    )
)
model.add( keras.layers.Flatten() )
model.add( keras.layers.Dropout(0.2) )

model.add( keras.layers.Dense(
    21, activation='softmax',
    )
)

model.summary()
model.compile(loss='binary_crossentropy',
              optimizer=keras.optimizers.Adamax(lr=0.001),
              metrics=['acc'])
model.fit_generator(
    train_dataset, 
    validation_data = val_dataset,
    workers=10,
    epochs=10,
)

# serialize model to JSON https://machinelearningmastery.com/save-load-keras-deep-learning-models/
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")


StarcoderdataJupyterScriptsDedupFiltered