<jupyter_start><jupyter_text>Major Neural Network Architectures Challenge *Data Science Unit 4 Sprint 3 Challenge*In this sprint challenge, you'll explore some of the cutting edge of Data Science. This week we studied several famous neural network architectures: recurrent neural networks (RNNs), long short-term memory (LSTMs), convolutional neural networks (CNNs), and Generative Adverserial Networks (GANs). In this sprint challenge, you will revisit these models. Remember, we are testing your knowledge of these architectures not your ability to fit a model with high accuracy. __*Caution:*__  these approaches can be pretty heavy computationally. All problems were designed so that you should be able to achieve results within at most 5-10 minutes of runtime on Colab or a comparable environment. If something is running longer, doublecheck your approach! Challenge Objectives*You should be able to:** Part 1: Train a RNN classification model* Part 2: Utilize a pre-trained CNN for objective detection* Part 3: Describe the components of an autoencoder* Part 4: Describe yourself as a Data Science and elucidate your vision of AI  Part 1 - RNNsUse an RNN/LSTM to fit a multi-class classification model on reuters news articles to distinguish topics of articles. The data is already encoded properly for use in an RNN model. Your Tasks: - Use Keras to fit a predictive model, classifying news articles into topics. - Report your overall score and accuracyFor reference, the [Keras IMDB sentiment classification example](https://github.com/keras-team/keras/blob/master/examples/imdb_lstm.py) will be useful, as well the RNN code we used in class.__*Note:*__  Focus on getting a running model, not on maxing accuracy with extreme data size or epoch numbers. Only revisit and push accuracy if you get everything else done!<jupyter_code>from tensorflow.keras.datasets import reuters

(X_train, y_train), (X_test, y_test) = reuters.load_data(num_words=None,
                                                         skip_top=0,
                                                         maxlen=None,
                                                         test_split=0.2,
                                                         seed=723812,
                                                         start_char=1,
                                                         oov_char=2,
                                                         index_from=3)
# Demo of encoding

word_index = reuters.get_word_index(path="reuters_word_index.json")

print(f"Iran is encoded as {word_index['iran']} in the data")
print(f"London is encoded as {word_index['london']} in the data")
print("Words are encoded as numbers in our dataset.")
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding, LSTM

batch_size = 46
max_features = len(word_index.values())
maxlen = 200

print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')

print('Pad sequences (samples x time)')
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)


print('Build model...')
# TODO - your code!
model = Sequential()
model.add(Embedding((max_features + 1), 128))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense((max_features + 1), activation='relu'))

# You should only run this cell once your model has been properly configured

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

print('Train...')
model.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=5,
          validation_data=(X_test, y_test))
score, acc = model.evaluate(X_test, y_test,
                            batch_size=batch_size)

print('Test score:', score)
print('Test accuracy:', acc)<jupyter_output>Train...
Train on 8982 samples, validate on 2246 samples
Epoch 1/5
8982/8982 [==============================] - 54s 6ms/sample - loss: 4.7331 - accuracy: 0.3927 - val_loss: 4.6994 - val_accuracy: 0.4840
Epoch 2/5
8982/8982 [==============================] - 49s 5ms/sample - loss: 4.8004 - accuracy: 0.5063 - val_loss: 5.6800 - val_accuracy: 0.4773
Epoch 3/5
8982/8982 [==============================] - 49s 6ms/sample - loss: 4.4634 - accuracy: 0.5374 - val_loss: 5.5159 - val_accuracy: 0.4016
Epoch 4/5
8982/8982 [==============================] - 52s 6ms/sample - loss: 4.2344 - accuracy: 0.5607 - val_loss: 4.3195 - val_accuracy: 0.5436
Epoch 5/5
8982/8982 [==============================] - 52s 6ms/sample - loss: 4.7035 - accuracy: 0.3611 - val_loss: 4.5545 - val_accuracy: 0.2409
2246/1 [=============================================================================================================================================================================================================[...]<jupyter_text>Sequence Data Question *Describe the `pad_sequences` method used on the training dataset. What does it do? Why do you need it?*_'pad_sequences'_ is used to ensure that the Sequences have the same length. It "Pads" the short sequences with some value, in this case, the default '0'. RNNs versus LSTMs *What are the primary motivations behind using Long-ShortTerm Memory Cell unit over traditional Recurrent Neural Networks?*LSTMs are used over RNNs because they address the 'Vanishing/Exploding Gradient Problems' that can occur with RNNs. RNN / LSTM Use Cases *Name and Describe 3 Use Cases of LSTMs or RNNs and why they are suited to that use case*1. Text Recognition 2. Speech Recognition 3. Text Classification RNN, and specifically LSTM are suited for the above cases (and many other language modeling problems) because of the ordered nature of language.   Part 2- CNNs Find the FrogTime to play "find the frog!" Use Keras and ResNet50 (pre-trained) to detect which of the following images contain frogs:<jupyter_code>!pip install google_images_download
from google_images_download import google_images_download

response = google_images_download.googleimagesdownload()
arguments = {"keywords": "lilly frog pond", "limit": 5, "print_urls": True}
absolute_image_paths = response.download(arguments)
absolute_image_paths<jupyter_output><empty_output><jupyter_text>At time of writing at least a few do, but since the Internet changes - it is possible your 5 won't. You can easily verify yourself, and (once you have working code) increase the number of images you pull to be more sure of getting a frog. Your goal is to validly run ResNet50 on the input images - don't worry about tuning or improving the model.*Hint* - ResNet 50 doesn't just return "frog". The three labels it has for frogs are: `bullfrog, tree frog, tailed frog`*Stretch goals* - Check for fish or other labels- Create a matplotlib visualizations of the images and your prediction as the visualization label<jupyter_code># You've got something to do in this cell. ;)

import numpy as np

from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions

def process_img_path(img_path):
  return image.load_img(img_path, target_size=(224, 224))

def img_contains_frog(img):
    """ Scans image for Frogs
    
    Should return a boolean (True/False) if a frog is in the image.
    
    Inputs:
    ---------
    img:  Precrossed image ready for prediction. The `process_img_path`             function should already be applied to the image. 
    
    Returns: 
    ---------
    frogs (boolean):  TRUE or FALSE - There are frogs in the image.
    
    """
    # Your Code Here
    # TODO - your code!
    X = image.img_to_array(img)
    X = np.expand_dims(X, axis=0)
    X = preprocess_input(X)
    model = ResNet50(weights='imagenet')
    features = model.predict(X)
    results = decode_predictions(features, top=3)[0]
    print(results, len(results))
    for entry in results:
        if entry[1] == 'bullfrog' or entry[1] == 'tree_frog' or entry[1] == 'tailed_frog':
            return print('YES! There are frogs in the image')
#         else:
#             return print('NO! There are no frogs in the image')
    return print('NO! There are no frogs in the image')
img_contains_frog(process_img_path('/home/ubuntu/Github/DS-Unit-4-Sprint-3-Deep-Learning/downloads/lilly frog pond/1.frog-lily-pond-2-b.jpg'))

from IPython.display import Image
Image(filename='/home/ubuntu/Github/DS-Unit-4-Sprint-3-Deep-Learning/downloads/lilly frog pond/1.frog-lily-pond-2-b.jpg', width=600)
img_contains_frog(process_img_path('/home/ubuntu/Github/DS-Unit-4-Sprint-3-Deep-Learning/downloads/lilly frog pond/2.frog-2504507_960_720.jpg'))
img_contains_frog(process_img_path('/home/ubuntu/Github/DS-Unit-4-Sprint-3-Deep-Learning/downloads/lilly frog pond/3.Frog-on-lily-pad.jpg'))
img_contains_frog(process_img_path('/home/ubuntu/Github/DS-Unit-4-Sprint-3-Deep-Learning/downloads/lilly frog pond/4.9a49083d4d7458a194a451eea757a444.jpg'))
img_contains_frog(process_img_path('/home/ubuntu/Github/DS-Unit-4-Sprint-3-Deep-Learning/downloads/lilly frog pond/5.Ponds5.jpg'))<jupyter_output>[('n03991062', 'pot', 0.1506036), ('n12620546', 'hip', 0.09164858), ('n01773797', 'garden_spider', 0.06739452)] 3
NO! There are no frogs in the image<jupyter_text>Stretch Goal: Displaying Predictions<jupyter_code>import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Part 3 - AutoencodersDescribe a use case for an autoencoder given that an autoencoder tries to predict its own input. __*Your Answer: One use case for an autoencoder is Reverse image searching. The image is encoded then reconstructed by the autoencoder. This reconstructed (simplified) image can then be used for similarity retrievals in the chosen training dataset.*__   Part 4 - More...<jupyter_code>import requests

image_urls = ["http://www.slrobertson.com/images/usa/georgia/atlanta/atl-botanical-gardens/frog-lily-pond-2-b.jpg",
              "https://cdn.pixabay.com/photo/2017/07/14/17/44/frog-2504507_960_720.jpg",
              "https://www.welcomewildlife.com/wp-content/uploads/2015/01/Frog-on-lily-pad.jpg",
              "https://i.pinimg.com/originals/9a/49/08/9a49083d4d7458a194a451eea757a444.jpg",
              "https://s3.amazonaws.com/assets.cce.cornell.edu/slides/22531/image/sized/Ponds5.jpg?1437939118"]

for _id,img in enumerate(image_urls): 
    r = requests.get(img)
    with open(f'example{_id}.jpg', 'wb') as f:
        f.write(r.content)<jupyter_output><empty_output><jupyter_text>Answer the following questions, with a target audience of a fellow Data Scientist:- What do you consider your strongest area, as a Data Scientist?        **My strongest area is Predictive Modeling. I find that I excel at the more detail oriented aspects of Data Science in general, and the feature engineering and hyperparameter optimization required to create efficient and accurate models is where I shine.**   - What area of Data Science would you most like to learn more about, and why?        **I would like to learn more about SQL. For how widespread it is in the Data science/analysis/engineering fields, I feel like we only scratched the surface during class and I would like to get more practice with it.**  - Where do you think Data Science will be in 5 years?        **I think that Machine learning is moving forawrd in leaps and bounds. Unfortunately with the geo-political climate that we find ourselves in, I think that a great many new data scientists will find themselves working with security applications, for both private and public entities.**  - What are the threats posed by AI to our society?        **Intelligence without discernment is a 18 wheeler without brakes. Once it gets going, only a catastrophic incident can stop it. It heartens me to think that the development of AI may lead to the end of almost every recorded disease that plagues the world today, but on the other end...Anthrax. A group of extremely intelligent men brought an end to the most devestating war in recorded history, by inventing the most devestating weapon in recorded history. What could an extremely intelligent group of computers do to/for the world?**  - How do you think we can counteract those threats?         **The same thing that we did during/after WW2, convince the greatest minds that curing diseases is better than creating them, and that freeing a populace is better than enslaving them. And getting these great minds to work on countering this threats as they arise, and giving them all the resources they would need to do so.**  - Do you think achieving General Artifical Intelligence is ever possible?        **In 2001, while I was a freshman in college, Columbia University was testing a Terabyte drive that was took up most of the wall in a room in a lab. In 2020 that'll be in a microsd card. I think that General Artificial Intelligence is pretty much inevitable at this point, as technology never takes backwards steps. Will we see it in my lifetime...I hope not, but it is definitely possible.**A few sentences per answer is fine - only elaborate if time allows.  Congratulations! Thank you for your hard work, and congratulations! You've learned a lot, and you should proudly call yourself a Data Scientist.<jupyter_code>from IPython.display import HTML

HTML("""<iframe src="https://giphy.com/embed/26xivLqkv86uJzqWk" width="480" height="270" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/mumm-champagne-saber-26xivLqkv86uJzqWk">via GIPHY</a></p>""")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10900887789259206, 'fraction_numerical': 0.03412894775141901, 'mean_word_length': 3.915236051502146, 'pattern_counts': {'":': 3, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 7, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '612425', 'license': 'MIT', 'n_tokens_mistral': 4286, 'n_tokens_neox': 3848, 'n_words': 1482, 'path': 'LS_DS_Uni_4_Sprint_3_Challenge.ipynb', 'repo_name': 'cicbeast/DS-Unit-4-Sprint-3-Deep-Learning', 'size': '13742'}