eaglelandsonce commited on
Commit
6d89f79
1 Parent(s): f2036f2

Update pages/13_TransferLearning.py

Browse files
Files changed (1) hide show
  1. pages/13_TransferLearning.py +16 -38
pages/13_TransferLearning.py CHANGED
@@ -1,13 +1,21 @@
1
  import streamlit as st
2
  import tensorflow as tf
3
  from tensorflow.keras import layers, models, applications
4
- from tensorflow.keras.preprocessing.image import ImageDataGenerator
5
  import matplotlib.pyplot as plt
6
- import numpy as np
7
 
8
- # Set dataset paths
9
- train_dir = 'data/train'
10
- validation_dir = 'data/validation'
 
 
 
 
 
 
 
 
 
11
 
12
  # Streamlit app
13
  st.title("Transfer Learning with VGG16 for Image Classification")
@@ -16,34 +24,6 @@ st.title("Transfer Learning with VGG16 for Image Classification")
16
  batch_size = st.slider("Batch Size", 16, 128, 32, 16)
17
  epochs = st.slider("Epochs", 5, 50, 10, 5)
18
 
19
- # Data augmentation and preprocessing
20
- train_datagen = ImageDataGenerator(
21
- rescale=1./255,
22
- rotation_range=40,
23
- width_shift_range=0.2,
24
- height_shift_range=0.2,
25
- shear_range=0.2,
26
- zoom_range=0.2,
27
- horizontal_flip=True,
28
- fill_mode='nearest'
29
- )
30
-
31
- validation_datagen = ImageDataGenerator(rescale=1./255)
32
-
33
- train_generator = train_datagen.flow_from_directory(
34
- train_dir,
35
- target_size=(150, 150),
36
- batch_size=batch_size,
37
- class_mode='binary'
38
- )
39
-
40
- validation_generator = validation_datagen.flow_from_directory(
41
- validation_dir,
42
- target_size=(150, 150),
43
- batch_size=batch_size,
44
- class_mode='binary'
45
- )
46
-
47
  # Load the pre-trained VGG16 model
48
  base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
49
 
@@ -70,11 +50,9 @@ model.compile(optimizer='adam',
70
  if st.button("Train Model"):
71
  with st.spinner("Training the model..."):
72
  history = model.fit(
73
- train_generator,
74
- steps_per_epoch=train_generator.samples // train_generator.batch_size,
75
  epochs=epochs,
76
- validation_data=validation_generator,
77
- validation_steps=validation_generator.samples // validation_generator.batch_size
78
  )
79
 
80
  st.success("Model training completed!")
@@ -100,5 +78,5 @@ if st.button("Train Model"):
100
 
101
  # Evaluate the model
102
  if st.button("Evaluate Model"):
103
- test_loss, test_acc = model.evaluate(validation_generator, verbose=2)
104
  st.write(f"Validation accuracy: {test_acc}")
 
1
  import streamlit as st
2
  import tensorflow as tf
3
  from tensorflow.keras import layers, models, applications
4
+ import tensorflow_datasets as tfds
5
  import matplotlib.pyplot as plt
 
6
 
7
+ # Load the dataset
8
+ dataset_name = "cats_vs_dogs"
9
+ (ds_train, ds_val), ds_info = tfds.load(dataset_name, split=['train[:80%]', 'train[80%:]'], with_info=True, as_supervised=True)
10
+
11
+ # Preprocess the dataset
12
+ def preprocess_image(image, label):
13
+ image = tf.image.resize(image, (150, 150))
14
+ image = image / 255.0
15
+ return image, label
16
+
17
+ ds_train = ds_train.map(preprocess_image).batch(32).prefetch(tf.data.AUTOTUNE)
18
+ ds_val = ds_val.map(preprocess_image).batch(32).prefetch(tf.data.AUTOTUNE)
19
 
20
  # Streamlit app
21
  st.title("Transfer Learning with VGG16 for Image Classification")
 
24
  batch_size = st.slider("Batch Size", 16, 128, 32, 16)
25
  epochs = st.slider("Epochs", 5, 50, 10, 5)
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  # Load the pre-trained VGG16 model
28
  base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
29
 
 
50
  if st.button("Train Model"):
51
  with st.spinner("Training the model..."):
52
  history = model.fit(
53
+ ds_train,
 
54
  epochs=epochs,
55
+ validation_data=ds_val
 
56
  )
57
 
58
  st.success("Model training completed!")
 
78
 
79
  # Evaluate the model
80
  if st.button("Evaluate Model"):
81
+ test_loss, test_acc = model.evaluate(ds_val, verbose=2)
82
  st.write(f"Validation accuracy: {test_acc}")