Giter Site home page Giter Site logo

abhi8893 / tensorflow-deep-learning-mrdbourke Goto Github PK

View Code? Open in Web Editor NEW
25.0 25.0 4.0 79.21 MB

Exploring mrdbourke's awesome ๐Ÿ”ฅ course on Deep Learning using Tensorflow. Beautiful Jupyter notebooks, a well structured installable Python package, issues tracking on a Kanban board, and a deployed Github Page website. Come dive in!

Python 0.42% Jupyter Notebook 99.58%
computer-vision deeplearning natural-language-processing tensorflow time-series-forecasting

tensorflow-deep-learning-mrdbourke's Introduction

Hi there ๐Ÿ‘‹

  • ๐Ÿ”ญ Iโ€™m currently working on creating a lit ๐Ÿ”ฅ Github profile. Be on the lookout! ๐Ÿ‘€
  • ๐ŸŽ“ I studied Applied Statistics at Indian Statistical Institute, where my Research Project was on the Application of Deep Learning in Natural Language Processing.
  • โœ”๏ธ I am a Tensorflow Certified Developer

Meanwhile checkout my repositories below ๐Ÿ‘‡

tensorflow-deep-learning-mrdbourke's People

Contributors

abhi8893 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tensorflow-deep-learning-mrdbourke's Issues

Notebook 03A: Pizza-Steak-Classifier

Tasks

  • Create ClassicImageDatasetDirectory
  • Download pizza-steak data (Create script)
  • Explore and visualize the data
  • CNN layers exploration
  • Binary Classification steps + CNN Model info
  • Implement multiple models: CNN, Dense, TinyVGG
  • Experiment with Data Augmentation

`ImageDataset.view_random_images` plots repeated images

Reproduce BUG:

from src.image import ClassicImageDataDirectory
import numpy as np
import matplotlib.pyplot as plt

data_dir = '../data/pizza_steak/'
imgdir = ClassicImageDataDirectory(data_dir, target_image_size=(224, 224), dtype=np.uint8)

datagen = imgdir.load(4)
batch = next(datagen)
batch.view_random_images(class_names='all', n_each=3)
plt.show()

44_repeated-class-image-plot

Create a class for evaluating classification model prediction

class ClassificationEvaluator:
    
    def __init__(self, train_preds, test_preds):
        pass
        
        

Features:

  • Create a show_prediction function for better focused viewing of model predictions
    • kind: [tp, fp, tn, fn]
    • conf: [low, medium, high]
def show_prediction(kind, conf):
    pass

Notebook 01A: Can't fit SLR with tensorflow using iterative optimizer steps

intercept = tf.Variable(polymodel.params[0], dtype=tf.float32)
slope = tf.Variable(polymodel.params[1], dtype=tf.float32)

def predict(intercept, slope, X):
    m = PolynomialModel([intercept, slope], 0)
    return m(X)

def mse(labels, preds):
    return tf.keras.losses.mse(tf.squeeze(labels), tf.squeeze(preds))

def loss_func(intercept, slope, X, y):
    predictions = predict(intercept, slope, X)
    return mse(y, predictions)

with tf.GradientTape() as tape:
    tape.watch(intercept)
    tape.watch(slope)

opt = tf.keras.optimizers.Adam(lr=0.05)
for i in range(20):
    opt.minimize(lambda : loss_func(intercept, slope, X, y), 
                 var_list=[intercept, slope])

Traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-3ef69543ef29> in <module>
     19 opt = tf.keras.optimizers.Adam(learning_rate=0.01)
     20 for i in range(20):
---> 21     opt.minimize(lambda : loss_func(intercept, slope, X, y), var_list=[intercept, slope])

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py in minimize(self, loss, var_list, grad_loss, name, tape)
    496     grads_and_vars = self._compute_gradients(
    497         loss, var_list=var_list, grad_loss=grad_loss, tape=tape)
--> 498     return self.apply_gradients(grads_and_vars, name=name)
    499 
    500   def _compute_gradients(self, loss, var_list, grad_loss=None, tape=None):

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py in apply_gradients(self, grads_and_vars, name, experimental_aggregate_gradients)
    596       RuntimeError: If called in a cross-replica context.
    597     """
--> 598     grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
    599     var_list = [v for (_, v) in grads_and_vars]
    600 

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\optimizer_v2\utils.py in filter_empty_gradients(grads_and_vars)
     77   if not filtered:
     78     raise ValueError("No gradients provided for any variable: %s." %
---> 79                      ([v.name for _, v in grads_and_vars],))
     80   if vars_with_empty_grads:
     81     logging.warning(

ValueError: No gradients provided for any variable: ['Variable:0', 'Variable:0'].

Notebook 05 - Transer Learning Part 2 - Fine Tuning

Tasks:

  • Download 1% of the 10 food classes dataset (download script)
  • Build models with Keras functional API
  • Compare 5 models:
    • Model 0: a transfer learning model using the Keras Functional API
    • Model 1: a feature extraction transfer learning model on 1% of the data with data augmentation
    • Model 2: a feature extraction transfer learning model on 10% of the data with data augmentation
    • Model 3: a fine-tuned transfer learning model on 10% of the data
    • Model 4: a fine-tuned transfer learning model on 100% of the data
  • Compare model experiments using TensorBoard

Notebook 08 - NLP Basics in Tensorflow

Tasks:

  • Download a text dataset (twitter disaster tweets) - Binary Classification
  • Visualize and explore text data
  • Tokenize our dataset (Explore various preprocessing options)
  • Create a baseline model with TF-IDF and Naive Bayes
  • Build several deep learning models
    • Simple Dense
    • LSTM
    • GRU
    • Conv1D
    • Transfer Learning
  • Compare the performance of our models
  • Combining our models into an ensemble
  • Find the most wrong predictions

Shuffling of data points in batches not only wrt labels but also the image features

Generally, what I want to define the shuffling in such a way that each batch is a good mix and represents the population distribution

  • Not just in terms of class labels, but in terms of the features of the data

For e.g., if we have 10k images -> 5k pizza, 5k steak. So each batch of 32 should ideally have 16 pizza and 16 steak. But should all pizza be pepporini and all steak be same kind of steak. In real world we would not have labels beyond class labels, but we can cluster the images and sample based on the cluster.

Change src package structure

โ””โ”€โ”€ src/
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ evaluate
    โ”œโ”€โ”€ image
    โ”œโ”€โ”€ models
    โ”œโ”€โ”€ preprocessing
    โ”œโ”€โ”€ text
    โ”œโ”€โ”€ tfplay
    โ”œโ”€โ”€ utils
    โ””โ”€โ”€ visualize

Notebooks to refactor:

  • 00
  • 01A
  • 01B
  • 01C
  • 02A
  • 02B
  • 03A

Input layer incompatible with Data Augmentation layer

inputs = tf.keras.layers.Input(shape=(224, 224, 3), name="input_layer")

data_augmentation = tf.keras.Sequential([
  preprocessing.RandomFlip("horizontal"),
  preprocessing.RandomRotation(0.2),
  preprocessing.RandomZoom(0.2),
  preprocessing.RandomHeight(0.2),
  preprocessing.RandomWidth(0.2),
], name ="data_augmentation")

data_augmentation(inputs)

Traceback:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-94-7923874bb0d7> in <module>
     10 ], name ="data_augmentation")
     11 
---> 12 data_augmentation(inputs)

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
    950     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
    951       return self._functional_construction_call(inputs, args, kwargs,
--> 952                                                 input_list)
    953 
    954     # Maintains info about the `Layer.call` stack.

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1089         # Check input assumptions set after layer building, e.g. input shape.
   1090         outputs = self._keras_tensor_symbolic_call(
-> 1091             inputs, input_masks, args, kwargs)
   1092 
   1093         if outputs is None:

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
    820       return nest.map_structure(keras_tensor.KerasTensor, output_signature)
    821     else:
--> 822       return self._infer_output_signature(inputs, args, kwargs, input_masks)
    823 
    824   def _infer_output_signature(self, inputs, args, kwargs, input_masks):

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
    861           # TODO(kaftan): do we maybe_build here, or have we already done it?
    862           self._maybe_build(inputs)
--> 863           outputs = call_fn(inputs, *args, **kwargs)
    864 
    865         self._handle_activity_regularization(inputs, outputs)

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\engine\sequential.py in call(self, inputs, training, mask)
    387         kwargs['training'] = training
    388 
--> 389       outputs = layer(inputs, **kwargs)
    390 
    391       if len(nest.flatten(outputs)) != 1:

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
   1010         with autocast_variable.enable_auto_cast_variables(
   1011             self._compute_dtype_object):
-> 1012           outputs = call_fn(inputs, *args, **kwargs)
   1013 
   1014         if self._activity_regularizer:

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\layers\preprocessing\image_preprocessing.py in call(self, inputs, training)
    865 
    866     output = control_flow_util.smart_cond(training, random_rotated_inputs,
--> 867                                           lambda: inputs)
    868     output.set_shape(inputs.shape)
    869     return output

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\utils\control_flow_util.py in smart_cond(pred, true_fn, false_fn, name)
    113         pred, true_fn=true_fn, false_fn=false_fn, name=name)
    114   return smart_module.smart_cond(
--> 115       pred, true_fn=true_fn, false_fn=false_fn, name=name)
    116 
    117 

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\framework\smart_cond.py in smart_cond(pred, true_fn, false_fn, name)
     52   if pred_value is not None:
     53     if pred_value:
---> 54       return true_fn()
     55     else:
     56       return false_fn()

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\layers\preprocessing\image_preprocessing.py in random_rotated_inputs()
    859       return transform(
    860           inputs,
--> 861           get_rotation_matrix(angles, img_hd, img_wd),
    862           fill_mode=self.fill_mode,
    863           fill_value=self.fill_value,

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\keras\layers\preprocessing\image_preprocessing.py in get_rotation_matrix(angles, image_height, image_width, name)
    755             math_ops.cos(angles)[:, None],
    756             y_offset[:, None],
--> 757             array_ops.zeros((num_angles, 2), dtypes.float32),
    758         ],
    759         axis=1)

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
    199     """Call target, and fall back on dispatchers if there is a TypeError."""
    200     try:
--> 201       return target(*args, **kwargs)
    202     except (TypeError, ValueError):
    203       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\ops\array_ops.py in wrapped(*args, **kwargs)
   2817 
   2818   def wrapped(*args, **kwargs):
-> 2819     tensor = fun(*args, **kwargs)
   2820     tensor._is_zeros_tensor = True
   2821     return tensor

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\ops\array_ops.py in zeros(shape, dtype, name)
   2866           # Create a constant if it won't be very big. Otherwise create a fill
   2867           # op to prevent serialized GraphDefs from becoming too large.
-> 2868           output = _constant_if_small(zero, shape, dtype, name)
   2869           if output is not None:
   2870             return output

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\ops\array_ops.py in _constant_if_small(value, shape, dtype, name)
   2802 def _constant_if_small(value, shape, dtype, name):
   2803   try:
-> 2804     if np.prod(shape) < 1000:
   2805       return constant(value, shape=shape, dtype=dtype, name=name)
   2806   except TypeError:

<__array_function__ internals> in prod(*args, **kwargs)

~\anaconda3\envs\ds-py37\lib\site-packages\numpy\core\fromnumeric.py in prod(a, axis, dtype, out, keepdims, initial, where)
   3029     """
   3030     return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
-> 3031                           keepdims=keepdims, initial=initial, where=where)
   3032 
   3033 

~\anaconda3\envs\ds-py37\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     85                 return reduction(axis=axis, out=out, **passkwargs)
     86 
---> 87     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     88 
     89 

~\anaconda3\envs\ds-py37\lib\site-packages\tensorflow\python\framework\ops.py in __array__(self)
    853         "Cannot convert a symbolic Tensor ({}) to a numpy array."
    854         " This error may indicate that you're trying to pass a Tensor to"
--> 855         " a NumPy call, which is not supported".format(self.name))
    856 
    857   def __len__(self):

NotImplementedError: Cannot convert a symbolic Tensor (data_augmentation/random_rotation_10/rotation_matrix/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

Notebook 09 - SkimLit (PudMed 200k RCT) - Sequential Sentence Classification

Tasks:

  • Download text dataset (PubMed RCT200k from Github)
  • Preprocess our dataset for modelling
  • Basline model (TF-IDF + Naive Bayes)
  • Deep Models with different combinations of: token embeddings, character embeddings, pretrained embeddings, positional embeddings
  • Build a multimodal model (multiple kinds of data inputs) - Replicate https://arxiv.org/pdf/1612.05251.pdf
  • Find the most wrong predictions
  • Make predictions on PubMed abstracts (any from the website)

Create a simple plotting function for continous and categorical var distributions

From Notebook 01C:

fig, axn = plt.subplots(2, 3, figsize=(16, 8))

columns = {'cont': ['age', 'bmi'], 
           'cat': ['sex', 'children', 'smoker', 'region']}

axn_flat = axn.flatten()

i = 0
for vartype, varlist in columns.items():
    for var in varlist:
        
        ax = axn_flat[i]
        
        if vartype == 'cont':
            df[var].plot(kind='hist', ax=ax)
        else:
            d = df[var].value_counts()
            d.plot(kind='bar', ax=ax)
            
        ax.set_title(var, fontdict=dict(weight='bold', size=10))
            
        
        i += 1
    

Passing `class_names` as a list in `ImageDataset.view_random_image` raises ValueError

imgdir = ClassicImageDataDirectory(data_dir, target_image_size=IMAGE_DIM, dtype=np.uint8)
batch = imgdir.load(128)
imgen = next(batch)
imgen.view_random_images(class_names=np.random.choice(imgen.class_names, size=10, replace=False))

Traceback:

--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-88675e61ccb9> in <module>
      1 batch = imgdir.load(128)
      2 imgen = next(batch)
----> 3 imgen.view_random_images(class_names=np.random.choice(imgen.class_names, size=10, replace=False))

c:\users\bhati\google drive\projects\tensorflow-tutorial-daniel-bourke\src\image\dataset.py in view_random_images(self, class_names, n_each, subset, cmap)
     63 
     64 
---> 65         if class_names == 'any':
     66             class_names = [np.random.choice(self.class_names)]
     67         elif class_names == 'all':

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Function add Index to Dataframe

def add_index_to_dataframe(df, cols):
    pass
  • Check if index has a name
  • Check if column named index already exists
  • Add cols to an existing index in dataframe

Notebook 07 - Milestone Project 1: Food Vision Big

Tasks:

  • Using Tensorflow Datasets to download and explore the data (A way to dump the data somewhere?)
  • Create preprocessing functions for our data
  • Batching and preparing datasets (datasets are prefetched and preprocessed faster)
  • Create modelling callbacks
  • Setting up mixed precision training (requires gpu compute capability > 7.0)
  • Build a feature extraction model (transfer learning)
  • Fine tune the feature extraction model
  • Viewing the training results on TensorBoard

Keras fit output and history metrics differ

Refer Notebook 01B

# Fit the model
>>> history = tfmodel.fit(X, ys['linear'], epochs=10)

Output:

Epoch 1/10
32/32 [==============================] - 0s 781us/step - loss: 1457.4397 - mse: 1457.4397
Epoch 2/10
32/32 [==============================] - 0s 785us/step - loss: 40.0271 - mse: 40.0271
Epoch 3/10
32/32 [==============================] - 0s 725us/step - loss: 25.4607 - mse: 25.4607
Epoch 4/10
32/32 [==============================] - 0s 635us/step - loss: 25.9577 - mse: 25.9577
Epoch 5/10
32/32 [==============================] - 0s 646us/step - loss: 25.4081 - mse: 25.4081
Epoch 6/10
32/32 [==============================] - 0s 732us/step - loss: 24.5854 - mse: 24.5854
Epoch 7/10
32/32 [==============================] - 0s 685us/step - loss: 27.7506 - mse: 27.7506
Epoch 8/10
32/32 [==============================] - 0s 637us/step - loss: 25.3395 - mse: 25.3395
Epoch 9/10
32/32 [==============================] - 0s 611us/step - loss: 24.8364 - mse: 24.8364
Epoch 10/10
32/32 [==============================] - 0s 757us/step - loss: 25.0970 - mse: 25.0970
>>> history.history['mse']
[788.6161499023438,
 38.7478141784668,
 25.893997192382812,
 25.751623153686523,
 25.781696319580078,
 25.781963348388672,
 25.767667770385742,
 25.736785888671875,
 25.817962646484375,
 25.85219955444336]

Create a Linear Model data sampler

Create a linear model data sampler to generate linear model data for

  • any xrange
  • no of data points
  • any degree
  • a random noise component

Take cues for this example in Notebook 01A:

xmin, xmax = -10, 40 # range
m = 1000 # no of data points
X = np.expand_dims(np.linspace(xmin, xmax, m), axis=-1) # You can randomly sample as well

# Create the labels aka the outputs (dependent variable)
polymodels = {}
polymodels['linear'] = PolynomialModel([2, 3], 5)
polymodels['quadratic'] = PolynomialModel([1, 2, -0.1], 10)

# Now sample some y
ys = {}
ys['linear'] = polymodels['linear'](X, add_noise=True)
ys['quadratic'] = polymodels['quadratic'](X, add_noise=True)

Add option for predetermined class_labels in src.visualize.LabelAnalyzer

https://github.com/abhi8893/Tensorflow-tutorial-Daniel-Bourke/blob/35869a3c5f37d1240698de7b9f0ace55f6a28a47/src/utils.py#L33-L42

class LabelAnalyzer: 
      
     def __init__(self, train_labels, test_labels=None, class_labels=None): 
         self.train = self._get_count_dict(train_labels) 
         self.__has_test_data = test_labels is not None 
          
         if self.__has_test_data: 
             self.test = self._get_count_dict(test_labels) 
  
         self._make_count_df() 

Assign attributes self.class_labels and self.labelmap in these cases:

  • If class_labels is None: estimate unique labels based on train and test data
  • if class_labels is list or tuple: Take this to be the true class_labels (Raise Error if length is different from estimated)
  • if class_labels is dict: keys as true class_labels, values as the estimate values

Lasso doesn't converge

From notebook 01C

pipe = Pipeline([
    ('preprocess', preprocess),
    ('scale', StandardScaler()),
    ('reg', LinearRegression())
])


# NOTE: Increasing tolerance to make Lasso converge (https://stackoverflow.com/questions/20681864/lasso-on-sklearn-does-not-converge)
param_grid = [{'reg': [LinearRegression()]},
              {'reg': [Ridge(), Lasso()], 'reg__alpha': [0.01, 0.1, 1, 10, 100]}] 


grid_search = GridSearchCV(pipe, param_grid, scoring='neg_mean_squared_error')
grid_search.fit(data_train, label_train)

Output warning:

C:\Users\bhati\anaconda3\envs\ds-py37\lib\site-packages\sklearn\linear_model\_coordinate_descent.py:532: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 892757429.305973, tolerance: 10875092.86663517
  positive)

Notebook 06 - Transfer Learning Part 3 - Food Vision Mini

Tasks:

  • Downloading and preparing 10% of the Food101 data (10% of training data)
  • Training a feature extraction transfer learning model on 10% of the Food101 training data
  • Fine-tuning our feature extraction model
  • Saving and loaded our trained model
  • Evaluating the performance of our Food Vision model trained on 10% of the training data
  • Finding our model's most wrong predictions
  • Making predictions with our Food Vision model on custom images of food

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.