You are on page 1of 5

23/03/2024, 18:10 Project code.

ipynb - Colaboratory

from tensorflow.keras.models import Sequential


from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
from google.colab.patches import cv2_imshow
import random
import cv2
from sklearn.model_selection import train_test_split

img1=cv2.imread("/content/black spot lemon.jpg");


img2=cv2.imread("/content/canker image.png");
img3=cv2.imread("/content/greening.png");
img4=cv2.imread("/content/healthy.png");
img5=cv2.imread("/content/melanose.jpg");

cv2_imshow(img1)

img1.shape

(256, 256, 3)

import pandas as pd
import numpy as np

print(img1.shape[0],img1.shape[1])

256 256

reshapeddata=img1.reshape(256,256*3)
reshapeddata

ndarray (256, 768) show data

https://colab.research.google.com/drive/12r2kLFDkDe3rkkn6xfiaVVPjgbxxNcZP#scrollTo=g033tHSHj1n4&printMode=true 1/5
23/03/2024, 18:10 Project code.ipynb - Colaboratory
def reshapeddata(img_rows, img_cols):
# Assuming reshapeddata() returns the necessary data
(X_train, y_train), (X_test, y_test) = reshapeddata()
# Assuming reshapeddata() provides the necessary data
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0
return (X_train, y_train), (X_test, y_test)
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], X_train.shape[2], 1))
X_test = X_test.reshape((X_test.shape[0],X_test.shape[1],X_test.shape[2],1))
print(X_train.shape)
print(X_test.shape)
X_train=X_train/255
X_test=X_test/255

type(reshapeddata)

function

model=Sequential()

model.add(Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)))

model.add(MaxPool2D(2,2))

model.add(Flatten())
model.add(Dense(100,activation='relu'))

model.add(Dense(10,activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

import tensorflow as tf
from tensorflow.keras import layers

def channel_shuffle(x, groups):


height, width, in_channels = x.shape.as_list()[1:]
channels_per_group = in_channels // groups
x = tf.reshape(x, [-1, height, width, groups, channels_per_group])
x = tf.transpose(x, [0, 1, 2, 4, 3])
x = tf.reshape(x, [-1, height, width, in_channels])
return x

def conv_bn_relu(inputs, filters, kernel_size, strides=1, padding='same', groups=1):


x = layers.Conv2D(filters, kernel_size, strides=strides, padding=padding, use_bias=False)(inputs)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
return x

https://colab.research.google.com/drive/12r2kLFDkDe3rkkn6xfiaVVPjgbxxNcZP#scrollTo=g033tHSHj1n4&printMode=true 2/5
23/03/2024, 18:10 Project code.ipynb - Colaboratory
def shuffle_unit(inputs, in_channels, out_channels, strides, groups):
shortcut = inputs

# 1x1 pointwise convolution


x = conv_bn_relu(inputs, in_channels // 4, 1, groups=groups)

# Channel shuffle
x = channel_shuffle(x, groups)

# Depthwise convolution
x = layers.DepthwiseConv2D(3, strides=strides, padding='same', use_bias=False)(x)
x = layers.BatchNormalization()(x)

# 1x1 pointwise convolution


x = conv_bn_relu(x, out_channels if strides > 1 else out_channels - in_channels, 1, groups=groups)

if strides == 2:
shortcut = layers.AveragePooling2D(pool_size=3, strides=2, padding='same')(shortcut)
shortcut = tf.keras.layers.Concatenate(axis=-1)([shortcut, tf.zeros_like(shortcut)])

output = layers.add([shortcut, x])


output = layers.ReLU()(output)
return output

def shuffle_net_v2(input_shape=(224, 224, 3), num_classes=1000, groups=3):


inputs = layers.Input(shape=input_shape)
x = conv_bn_relu(inputs, 24, 3, strides=2)

x = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(x)

x = shuffle_unit(x, 24, 240, strides=2, groups=groups)


x = shuffle_unit(x, 240, 480, strides=2, groups=groups)
x = shuffle_unit(x, 480, 960, strides=2, groups=groups)

x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(num_classes, activation='softmax')(x)
# Add a convolution layer to the input with the smaller number of channels
x = layers.Conv2D(240, kernel_size=1, strides=1, padding='same')(x)

# Now, the shapes of the two inputs will be the same and the error will be resolved.

model = tf.keras.Model(inputs, x)
return model

import matplotlib.pyplot as plt

from albumentations.augmentations.transforms import InvertImg


transform = InvertImg(p=1.0)
img1=cv2.imread("/content/black spot lemon.jpg");
img2=cv2.imread("/content/canker image.png");
img3=cv2.imread("/content/greening.png");
img4=cv2.imread("/content/healthy.png");
img5=cv2.imread("/content/melanose image.png");
augmented_image = transform(image=img1)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)

<matplotlib.image.AxesImage at 0x7ed3cc140ac0>

https://colab.research.google.com/drive/12r2kLFDkDe3rkkn6xfiaVVPjgbxxNcZP#scrollTo=g033tHSHj1n4&printMode=true 3/5
23/03/2024, 18:10 Project code.ipynb - Colaboratory
from albumentations.augmentations.transforms import Solarize
transform = Solarize(threshold=70, p=1.0)
augmented_image = transform(image=img1)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)

<matplotlib.image.AxesImage at 0x7ed3cc3dcee0>

from albumentations.augmentations.transforms import Solarize


transform = Solarize(threshold=70, p=1.0)
augmented_image = transform(image=img2)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)

<matplotlib.image.AxesImage at 0x7ed3cc46fdf0>

from albumentations.augmentations.transforms import Solarize


transform = Solarize(threshold=100, p=1.0)
augmented_image = transform(image=img3)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)

<matplotlib.image.AxesImage at 0x7ed3cc0d6cb0>

https://colab.research.google.com/drive/12r2kLFDkDe3rkkn6xfiaVVPjgbxxNcZP#scrollTo=g033tHSHj1n4&printMode=true 4/5
23/03/2024, 18:10 Project code.ipynb - Colaboratory
from albumentations.augmentations.transforms import Solarize
transform = Solarize(threshold=70, p=1.0)
augmented_image = transform(image=img4)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)

<matplotlib.image.AxesImage at 0x7ed3cc1f80a0>

from albumentations.augmentations.transforms import Solarize


transform = Solarize(threshold=180, p=1.0)

https://colab.research.google.com/drive/12r2kLFDkDe3rkkn6xfiaVVPjgbxxNcZP#scrollTo=g033tHSHj1n4&printMode=true 5/5

You might also like