Skip to content
Snippets Groups Projects
Commit ff60ae2e authored by Byungheon Jeong's avatar Byungheon Jeong
Browse files

differ image entry

parent 670a1c6c
No related branches found
No related tags found
No related merge requests found
Pipeline #12407 passed
%% Cell type:code id: tags:
``` python
from numpy import loadtxt
from keras.models import load_model
import tensorflow as tf
from keras import backend as K
```
%% Cell type:code id: tags:
``` python
!pwd
```
%% Output
/userdata/kerasData
%% Cell type:code id: tags:
``` python
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
"""Precision metric.
Only computes a batch-wise average of precision.
Computes the precision, a metric for multi-label classification of
how many selected items are relevant.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
precision = precision(y_true, y_pred)
recall = recall(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
```
%% Cell type:code id: tags:
``` python
INIT_LR = 1e-4
batch_size = 16
NUM_EPOCHS = 200
image_size=(128, 128)
class_mode="binary"
image_generator = tf.keras.preprocessing.image.ImageDataGenerator()
TestGenerator = image_generator.flow_from_directory(
"/userdata/kerasData/preloaded/flowDirectory/validation",
target_size=image_size,
batch_size=batch_size,
seed=1000,
class_mode=class_mode)
```
%% Output
Found 2781 images belonging to 2 classes.
%% Cell type:code id: tags:
``` python
model_128 = load_model("/userdata/kerasData/output/model/best_model128_128_e-4.h5", custom_objects={"f1":f1})
model_128.summary()
```
%% Output
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
separable_conv2d (SeparableC (None, 128, 128, 16) 211
_________________________________________________________________
activation (Activation) (None, 128, 128, 16) 0
_________________________________________________________________
batch_normalization (BatchNo (None, 128, 128, 16) 64
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 64, 64, 16) 0
_________________________________________________________________
separable_conv2d_1 (Separabl (None, 64, 64, 32) 688
_________________________________________________________________
activation_1 (Activation) (None, 64, 64, 32) 0
_________________________________________________________________
batch_normalization_1 (Batch (None, 64, 64, 32) 128
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 32, 32) 0
_________________________________________________________________
separable_conv2d_2 (Separabl (None, 32, 32, 64) 2400
_________________________________________________________________
activation_2 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
batch_normalization_2 (Batch (None, 32, 32, 64) 256
_________________________________________________________________
separable_conv2d_3 (Separabl (None, 32, 32, 64) 4736
_________________________________________________________________
activation_3 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
batch_normalization_3 (Batch (None, 32, 32, 64) 256
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 16, 16, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 16384) 0
_________________________________________________________________
dense (Dense) (None, 128) 2097280
_________________________________________________________________
activation_4 (Activation) (None, 128) 0
_________________________________________________________________
batch_normalization_4 (Batch (None, 128) 512
_________________________________________________________________
dropout (Dropout) (None, 128) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 16512
_________________________________________________________________
activation_5 (Activation) (None, 128) 0
_________________________________________________________________
batch_normalization_5 (Batch (None, 128) 512
_________________________________________________________________
dropout_1 (Dropout) (None, 128) 0
_________________________________________________________________
dense_2 (Dense) (None, 1) 129
_________________________________________________________________
activation_6 (Activation) (None, 1) 0
=================================================================
Total params: 2,123,684
Trainable params: 2,122,820
Non-trainable params: 864
_________________________________________________________________
%% Cell type:code id: tags:
``` python
model_128.evaluate(TestGenerator)
```
%% Output
174/174 [==============================] - 419s 2s/step - loss: 0.7333 - accuracy: 0.5045 - f1: 0.6095 - recall: 0.8166 - precision: 0.5016
[0.7332570552825928,
0.5044947862625122,
0.6094719767570496,
0.8166065216064453,
0.5015521049499512]
%% Cell type:code id: tags:
``` python
INIT_LR = 1e-4
batch_size = 16
NUM_EPOCHS = 200
image_size=(2048, 1536)
class_mode="categorical"
model_e5 = load_model('/userdata/kerasData/best_model_e5.h5', custom_objects={"f1":f1})
model_e5.summary()
image_generator = tf.keras.preprocessing.image.ImageDataGenerator()
testGenerator = image_generator.flow_from_directory(
"/userdata/kerasData/preloaded/flowDirectory/test",
target_size=image_size,
seed=1000,
batch_size=batch_size,
class_mode=class_mode)
```
%% Output
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
separable_conv2d (SeparableC (None, 128, 128, 16) 211
_________________________________________________________________
activation (Activation) (None, 128, 128, 16) 0
_________________________________________________________________
batch_normalization (BatchNo (None, 128, 128, 16) 64
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 64, 64, 16) 0
_________________________________________________________________
separable_conv2d_1 (Separabl (None, 64, 64, 32) 688
_________________________________________________________________
activation_1 (Activation) (None, 64, 64, 32) 0
_________________________________________________________________
batch_normalization_1 (Batch (None, 64, 64, 32) 128
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 32, 32) 0
_________________________________________________________________
separable_conv2d_2 (Separabl (None, 32, 32, 64) 2400
_________________________________________________________________
activation_2 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
batch_normalization_2 (Batch (None, 32, 32, 64) 256
_________________________________________________________________
separable_conv2d_3 (Separabl (None, 32, 32, 64) 4736
_________________________________________________________________
activation_3 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
batch_normalization_3 (Batch (None, 32, 32, 64) 256
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 16, 16, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 16384) 0
_________________________________________________________________
dense (Dense) (None, 128) 2097280
_________________________________________________________________
activation_4 (Activation) (None, 128) 0
_________________________________________________________________
batch_normalization_4 (Batch (None, 128) 512
_________________________________________________________________
dropout (Dropout) (None, 128) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 16512
_________________________________________________________________
activation_5 (Activation) (None, 128) 0
_________________________________________________________________
batch_normalization_5 (Batch (None, 128) 512
_________________________________________________________________
dropout_1 (Dropout) (None, 128) 0
_________________________________________________________________
dense_2 (Dense) (None, 2) 258
_________________________________________________________________
activation_6 (Activation) (None, 2) 0
=================================================================
Total params: 2,123,813
Trainable params: 2,122,949
Non-trainable params: 864
_________________________________________________________________
Found 3485 images belonging to 2 classes.
%% Cell type:code id: tags:
``` python
image_size=(2048, 1536)
class_mode="categorical"
image_generator = tf.keras.preprocessing.image.ImageDataGenerator()
testGenerator = image_generator.flow_from_directory(
"/userdata/kerasData/preloaded/flowDirectory/test",
target_size=image_size,
seed=1000,
batch_size=batch_size,
class_mode=class_mode)
```
%% Output
Found 3485 images belonging to 2 classes.
%% Cell type:code id: tags:
``` python
model_e5.evaluate(testGenerator)
```
%% Output
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-8-6de600d60988> in <module>
----> 1 model_e5.evaluate(testGenerator)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
64 def _method_wrapper(self, *args, **kwargs):
65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
---> 66 return method(self, *args, **kwargs)
67
68 # Running inside `run_distribute_coordinator` already.
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in evaluate(self, x, y, batch_size, verbose, sample_weight, steps, callbacks, max_queue_size, workers, use_multiprocessing, return_dict)
1079 step_num=step):
1080 callbacks.on_test_batch_begin(step)
-> 1081 tmp_logs = test_function(iterator)
1082 # Catch OutOfRangeError for Datasets of unknown size.
1083 # This blocks until the batch has finished executing.
/opt/conda/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
578 xla_context.Exit()
579 else:
--> 580 result = self._call(*args, **kwds)
581
582 if tracing_count == self._get_tracing_count():
/opt/conda/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
642 # Lifting succeeded, so variables are initialized and we can run the
643 # stateless function.
--> 644 return self._stateless_fn(*args, **kwds)
645 else:
646 canon_args, canon_kwds = \
/opt/conda/lib/python3.6/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
2418 with self._lock:
2419 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2420 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2421
2422 @property
/opt/conda/lib/python3.6/site-packages/tensorflow/python/eager/function.py in _filtered_call(self, args, kwargs)
1663 if isinstance(t, (ops.Tensor,
1664 resource_variable_ops.BaseResourceVariable))),
-> 1665 self.captured_inputs)
1666
1667 def _call_flat(self, args, captured_inputs, cancellation_manager=None):
/opt/conda/lib/python3.6/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1744 # No tape is watching; skip to running the function.
1745 return self._build_call_outputs(self._inference_function.call(
-> 1746 ctx, args, cancellation_manager=cancellation_manager))
1747 forward_backward = self._select_forward_and_backward_functions(
1748 args,
/opt/conda/lib/python3.6/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancellation_manager)
596 inputs=args,
597 attrs=attrs,
--> 598 ctx=ctx)
599 else:
600 outputs = execute.execute_with_cancellation(
/opt/conda/lib/python3.6/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Incompatible shapes: [16,2] vs. [3072,2]
[[node mul_1 (defined at <ipython-input-2-85a3f15ecf4d>:24) ]]
[[assert_less_equal/Assert/AssertGuard/pivot_f/_13/_39]]
(1) Invalid argument: Incompatible shapes: [16,2] vs. [3072,2]
[[node mul_1 (defined at <ipython-input-2-85a3f15ecf4d>:24) ]]
0 successful operations.
0 derived errors ignored. [Op:__inference_test_function_2377]
Errors may have originated from an input operation.
Input Source operations connected to node mul_1:
sequential/activation_6/Softmax (defined at <ipython-input-8-6de600d60988>:1)
Input Source operations connected to node mul_1:
sequential/activation_6/Softmax (defined at <ipython-input-8-6de600d60988>:1)
Function call stack:
test_function -> test_function
%% Cell type:code id: tags:
``` python
```
This diff is collapsed.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment