This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Install Keras Tuner | |
!pip install -U keras-tuner | |
import kerastuner as kt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generator = generator_model() | |
discriminator = discriminator_model() | |
generator_optimizer = tf.keras.optimizers.Adam(0.0002,beta_1=0.5) | |
discriminator_optimizer = tf.keras.optimizers.Adam(0.0002, beta_1=0.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tf.function | |
def train_step(images, generator, discriminator, generator_optimizer, discriminator_optimizer): | |
noise = tf.random.normal([BATCH_SIZE, NOISE_DIM]) | |
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape: | |
generated_images = generator(noise, training=True) | |
real_output = discriminator(images, training=True) | |
fake_output = discriminator(generated_images, training=True) | |
gen_loss = generator_loss(fake_output) | |
disc_loss = discriminator_loss(real_output, fake_output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig, ax = plt.subplots() | |
losses = np.array(losses) | |
plt.plot(losses.T[0], label='Discriminator', alpha=0.5) | |
plt.plot(losses.T[1], label='Generator', alpha=0.5) | |
plt.title("Training Losses") | |
plt.legend() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def discriminator_model(): | |
model = tf.keras.Sequential() | |
model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same', input_shape=[32, 32, 3])) | |
model.add(layers.LeakyReLU()) | |
model.add(layers.Conv2D(256, (5, 5), strides=(2, 2), padding='same')) | |
model.add(layers.BatchNormalization()) | |
model.add(layers.LeakyReLU()) | |
model.add(layers.Conv2D(512, (5, 5), strides=(2, 2), padding='same')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generator_model(): | |
model = tf.keras.Sequential() | |
model.add(layers.Dense(4*4*512, use_bias=False, input_shape=(100,))) | |
model.add(layers.BatchNormalization()) | |
model.add(layers.LeakyReLU()) | |
model.add(layers.Reshape((4, 4, 512))) | |
assert model.output_shape == (None, 4, 4, 512) # Note: None is the batch size | |
model.add(layers.Conv2DTranspose(256, (5, 5), strides=(2, 2), padding='same', use_bias=False)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def scale(x, feature_range=(-1, 1)): | |
# scale to (0, 1) | |
x = ((x - x.min())/(255 - x.min())) | |
# scale to feature_range | |
min, max = feature_range | |
x = x * (max - min) + min | |
return x | |
class Dataset: | |
def __init__(self, train, test, val_frac=0.5, shuffle=False, scale_func=None): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.request import urlretrieve | |
from os.path import isfile, isdir | |
from tqdm import tqdm | |
!mkdir data | |
data_dir = 'data/' | |
if not isdir(data_dir): | |
raise Exception("Data directory doesn't exist!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private lateinit var mClassifier: Classifier | |
private lateinit var mBitmap: Bitmap | |
private val mCameraRequestCode = 0 | |
private val mGalleryRequestCode = 2 | |
private val mInputSize = 224 | |
private val mModelPath = "plant_disease_model.tflite" | |
private val mLabelPath = "plant_labels.txt" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun scaleImage(bitmap: Bitmap?): Bitmap { | |
val orignalWidth = bitmap!!.width | |
val originalHeight = bitmap.height | |
val scaleWidth = mInputSize.toFloat() / orignalWidth | |
val scaleHeight = mInputSize.toFloat() / originalHeight | |
val matrix = Matrix() | |
matrix.postScale(scaleWidth, scaleHeight) | |
return Bitmap.createBitmap(bitmap, 0, 0, orignalWidth, originalHeight, matrix, true) | |
} |