Kaggle competition: Petals to the Metal - Flower Classification on TPU
def parse_tweet(tweet):
tweet = tweet.lstrip().rstrip()
tweet = re.sub("http:\S+", "", tweet)
 tweet = re.sub("https:\S+", "", tweet)
 tweet = re.sub(r"[,.;
tweet = tweet.replace(" ", " ")
 tweet = tweet.lower()
tweet = tweet.replace('...', '...' ).strip()
tweet = tweet.replace("'", " ' " ).strip()
tweet = re.sub(r"\x89Û_", "", tweet)
 tweet = re.sub(r"\x89ÛÒ", "", tweet)
 tweet = re.sub(r"\x89ÛÓ", "", tweet)
 tweet = re.sub(r"\x89ÛÏWhen", "When", tweet)
 tweet = re.sub(r"\x89ÛÏ", "", tweet)
 tweet = re.sub(r"China\x89Ûªs", "China's", tweet)
 tweet = re.sub(r"let\x89Ûªs", "let's", tweet)
 tweet = re.sub(r"\x89Û÷", "", tweet)
 tweet = re.sub(r"\x89Ûª", "", tweet)
 tweet = re.sub(r"\x89Û\x9d", "", tweet)
 tweet = re.sub(r"å_", "", tweet)
 tweet = re.sub(r"\x89Û¢", "", tweet)
 tweet = re.sub(r"\x89Û¢åÊ", "", tweet)
 tweet = re.sub(r"fromåÊwounds", "from wounds", tweet)
 tweet = re.sub(r"åÊ", "", tweet)
 tweet = re.sub(r"åÈ", "", tweet)
 tweet = re.sub(r"JapÌ_n", "Japan", tweet)
 tweet = re.sub(r"Ì©", "e", tweet)
 tweet = re.sub(r"å¨", "", tweet)
 tweet = re.sub(r"SuruÌ¤", "Suruc", tweet)
 tweet = re.sub(r"he's", "he is", tweet)
 tweet = re.sub(r"there's", "there is", tweet)
 tweet = re.sub(r"We're", "We are", tweet)
 tweet = re.sub(r"That's", "That is", tweet)
 tweet = re.sub(r"won't", "will not", tweet)
 tweet = re.sub(r"they're", "they are", tweet)
 tweet = re.sub(r"Can't", "Cannot", tweet)
 tweet = re.sub(r"wasn't", "was not", tweet)
 tweet = re.sub(r"don\x89Ûªt", "do not", tweet)
 tweet = re.sub(r"aren't", "are not", tweet)
 tweet = re.sub(r"isn't", "is not", tweet)
 tweet = re.sub(r"What's", "What is", tweet)
 tweet = re.sub(r"haven't", "have not", tweet)
 tweet = re.sub(r"hasn't", "has not", tweet)
 tweet = re.sub(r"There's", "There is", tweet)
 tweet = re.sub(r"He's", "He is", tweet)
 tweet = re.sub(r"It's", "It is", tweet)
 tweet = re.sub(r"You're", "You are", tweet)
 tweet = re.sub(r"I'M", "I am", tweet)
 tweet = re.sub(r"shouldn't", "should not", tweet)
 tweet = re.sub(r"wouldn't", "would not", tweet)
 tweet = re.sub(r"i'm", "I am", tweet)
 tweet = re.sub(r"I\x89Ûªm", "I am", tweet)
 tweet = re.sub(r"I'm", "I am", tweet)
 tweet = re.sub(r"Isn't", "is not", tweet)
 tweet = re.sub(r"Here's", "Here is", tweet)
 tweet = re.sub(r"you've", "you have", tweet)
 tweet = re.sub(r"you\x89Ûªve", "you have", tweet)
 tweet = re.sub(r"we're", "we are", tweet)
 tweet = re.sub(r"what's", "what is", tweet)
 tweet = re.sub(r"couldn't", "could not", tweet)
 tweet = re.sub(r"we've", "we have", tweet)
 tweet = re.sub(r"it\x89Ûªs", "it is", tweet)
 tweet = re.sub(r"doesn\x89Ûªt", "does not", tweet)
 tweet = re.sub(r"It\x89Ûªs", "It is", tweet)
 tweet = re.sub(r"Here\x89Ûªs", "Here is", tweet)
 tweet = re.sub(r"who's", "who is", tweet)
 tweet = re.sub(r"I\x89Ûªve", "I have", tweet)
 tweet = re.sub(r"y'all", "you all", tweet)
 tweet = re.sub(r"can\x89Ûªt", "cannot", tweet)
 tweet = re.sub(r"would've", "would have", tweet)
 tweet = re.sub(r"it'll", "it will", tweet)
 tweet = re.sub(r"we'll", "we will", tweet)
 tweet = re.sub(r"wouldn\x89Ûªt", "would not", tweet)
 tweet = re.sub(r"We've", "We have", tweet)
 tweet = re.sub(r"he'll", "he will", tweet)
 tweet = re.sub(r"Y'all", "You all", tweet)
 tweet = re.sub(r"Weren't", "Were not", tweet)
 tweet = re.sub(r"Didn't", "Did not", tweet)
 tweet = re.sub(r"they'll", "they will", tweet)
 tweet = re.sub(r"they'd", "they would", tweet)
 tweet = re.sub(r"DON'T", "DO NOT", tweet)
 tweet = re.sub(r"That\x89Ûªs", "That is", tweet)
 tweet = re.sub(r"they've", "they have", tweet)
 tweet = re.sub(r"i'd", "I would", tweet)
 tweet = re.sub(r"should've", "should have", tweet)
 tweet = re.sub(r"You\x89Ûªre", "You are", tweet)
 tweet = re.sub(r"where's", "where is", tweet)
 tweet = re.sub(r"Don\x89Ûªt", "Do not", tweet)
 tweet = re.sub(r"we'd", "we would", tweet)
 tweet = re.sub(r"i'll", "I will", tweet)
 tweet = re.sub(r"weren't", "were not", tweet)
 tweet = re.sub(r"They're", "They are", tweet)
 tweet = re.sub(r"Can\x89Ûªt", "Cannot", tweet)
 tweet = re.sub(r"you\x89Ûªll", "you will", tweet)
 tweet = re.sub(r"I\x89Ûªd", "I would", tweet)
 tweet = re.sub(r"let's", "let us", tweet)
 tweet = re.sub(r"&gt;", ">", tweet)
 tweet = re.sub(r"&lt;", "<", tweet)
 tweet = re.sub(r"&amp;", "&", tweet)
 tweet = re.sub(r"w/e", "whatever", tweet)
 tweet = re.sub(r"w/", "with", tweet)
 tweet = re.sub(r"USAgov", "USA government", tweet)
 tweet = re.sub(r"recentlu", "recently", tweet)
 tweet = re.sub(r"Ph0tos", "Photos", tweet)
 tweet = re.sub(r"exp0sed", "exposed", tweet)
 tweet = re.sub(r"<3", "love", tweet)
 tweet = re.sub(r"amageddon", "armageddon", tweet)
 tweet = re.sub(r"Trfc", "Traffic", tweet)
 tweet = re.sub(r"8/5/2015", "2015-08-05", tweet)
 tweet = re.sub(r"chest/torso", "chest / torso", tweet)
 tweet = re.sub(r"WindStorm", "Wind Storm", tweet)
 tweet = re.sub(r"8/6/2015", "2015-08-06", tweet)
 tweet = re.sub(r"10:38PM", "10:38 PM", tweet)
 tweet = re.sub(r"10:30pm", "10:30 PM", tweet)
 tweet = re.sub(r"MH370:", "MH370 :", tweet)
 tweet = re.sub(r"PM:", "Prime Minister :", tweet)
 tweet = re.sub(r"Legionnaires:", "Legionnaires :", tweet)
 tweet = re.sub(r"Latest:", "Latest :", tweet)
 tweet = re.sub(r"Crash:", "Crash :", tweet)
 tweet = re.sub(r"News:", "News :", tweet)
 tweet = re.sub(r"derailment:", "derailment :", tweet)
 tweet = re.sub(r"attack:", "attack :", tweet)
 tweet = re.sub(r"Saipan:", "Saipan :", tweet)
 tweet = re.sub(r"Photo:", "Photo :", tweet)
 tweet = re.sub(r"Funtenna:", "Funtenna :", tweet)
 tweet = re.sub(r"quiz:", "quiz :", tweet)
 tweet = re.sub(r"VIDEO:", "VIDEO :", tweet)
 tweet = re.sub(r"MP:", "MP :", tweet)
 tweet = re.sub(r"UTC2015-08-05", "UTC 2015-08-05", tweet)
 tweet = re.sub(r"California:", "California :", tweet)
 tweet = re.sub(r"horror:", "horror :", tweet)
 tweet = re.sub(r"Past:", "Past :", tweet)
 tweet = re.sub(r"Time2015-08-06", "Time 2015-08-06", tweet)
 tweet = re.sub(r"here:", "here :", tweet)
 tweet = re.sub(r"fires.", "fires.", tweet)
 tweet = re.sub(r"Forest:", "Forest :", tweet)
 tweet = re.sub(r"Cramer:", "Cramer :", tweet)
 tweet = re.sub(r"Chile:", "Chile :", tweet)
 tweet = re.sub(r"link:", "link :", tweet)
 tweet = re.sub(r"crash:", "crash :", tweet)
 tweet = re.sub(r"Video:", "Video :", tweet)
 tweet = re.sub(r"Bestnaijamade:", "bestnaijamade :", tweet)
 tweet = re.sub(r"NWS:", "National Weather Service :", tweet)
 tweet = re.sub(r".caught", ".caught", tweet)
 tweet = re.sub(r"Hobbit:", "Hobbit :", tweet)
 tweet = re.sub(r"2015:", "2015 :", tweet)
 tweet = re.sub(r"post:", "post :", tweet)
 tweet = re.sub(r"BREAKING:", "BREAKING :", tweet)
 tweet = re.sub(r"Island:", "Island :", tweet)
 tweet = re.sub(r"Med:", "Med :", tweet)
 tweet = re.sub(r"97/Georgia", "97 / Georgia", tweet)
 tweet = re.sub(r"Here:", "Here :", tweet)
 tweet = re.sub(r"horror;", "horror ;", tweet)
 tweet = re.sub(r"people;", "people ;", tweet)
 tweet = re.sub(r"refugees;", "refugees ;", tweet)
 tweet = re.sub(r"Genocide;", "Genocide ;", tweet)
 tweet = re.sub(r".POTUS", ".POTUS", tweet)
 tweet = re.sub(r"Collision-No", "Collision - No", tweet)
 tweet = re.sub(r"Rear-", "Rear -", tweet)
 tweet = re.sub(r"Broadway:", "Broadway :", tweet)
 tweet = re.sub(r"Correction:", "Correction :", tweet)
 tweet = re.sub(r"UPDATE:", "UPDATE :", tweet)
 tweet = re.sub(r"Times:", "Times :", tweet)
 tweet = re.sub(r"RT:", "RT :", tweet)
 tweet = re.sub(r"Police:", "Police :", tweet)
 tweet = re.sub(r"Training:", "Training :", tweet)
 tweet = re.sub(r"Hawaii:", "Hawaii :", tweet)
 tweet = re.sub(r"Selfies:", "Selfies :", tweet)
 tweet = re.sub(r"Content:", "Content :", tweet)
 tweet = re.sub(r"101:", "101 :", tweet)
 tweet = re.sub(r"story:", "story :", tweet)
 tweet = re.sub(r"injured:", "injured :", tweet)
 tweet = re.sub(r"poll:", "poll :", tweet)
 tweet = re.sub(r"Guide:", "Guide :", tweet)
 tweet = re.sub(r"Update:", "Update :", tweet)
 tweet = re.sub(r"alarm:", "alarm :", tweet)
 tweet = re.sub(r"floods:", "floods :", tweet)
 tweet = re.sub(r"Flood:", "Flood :", tweet)
 tweet = re.sub(r"MH370;", "MH370 ;", tweet)
 tweet = re.sub(r"life:", "life :", tweet)
 tweet = re.sub(r"crush:", "crush :", tweet)
 tweet = re.sub(r"now:", "now :", tweet)
 tweet = re.sub(r"Vote:", "Vote :", tweet)
 tweet = re.sub(r"Catastrophe.", "Catastrophe.", tweet)
 tweet = re.sub(r"library:", "library :", tweet)
 tweet = re.sub(r"Bush:", "Bush :", tweet)
 tweet = re.sub(r";ACCIDENT", "; ACCIDENT", tweet)
 tweet = re.sub(r"accident:", "accident :", tweet)
 tweet = re.sub(r"Taiwan;", "Taiwan ;", tweet)
 tweet = re.sub(r"Map:", "Map :", tweet)
 tweet = re.sub(r"failure:", "failure :", tweet)
 tweet = re.sub(r"150-Foot", "150 - Foot", tweet)
 tweet = re.sub(r"failure:", "failure :", tweet)
 tweet = re.sub(r"prefer:", "prefer :", tweet)
 tweet = re.sub(r"CNN:", "CNN :", tweet)
 tweet = re.sub(r"Oops:", "Oops :", tweet)
 tweet = re.sub(r"Disco:", "Disco :", tweet)
 tweet = re.sub(r"Disease:", "Disease :", tweet)
 tweet = re.sub(r"Grows:", "Grows :", tweet)
 tweet = re.sub(r"projected:", "projected :", tweet)
 tweet = re.sub(r"Pakistan.", "Pakistan.", tweet)
 tweet = re.sub(r"ministers:", "ministers :", tweet)
 tweet = re.sub(r"Photos:", "Photos :", tweet)
 tweet = re.sub(r"Disease:", "Disease :", tweet)
 tweet = re.sub(r"pres:", "press :", tweet)
 tweet = re.sub(r"winds.", "winds.", tweet)
 tweet = re.sub(r"MPH.", "MPH.", tweet)
 tweet = re.sub(r"PHOTOS:", "PHOTOS :", tweet)
 tweet = re.sub(r"Time2015-08-05", "Time 2015-08-05", tweet)
 tweet = re.sub(r"Denmark:", "Denmark :", tweet)
 tweet = re.sub(r"Articles:", "Articles :", tweet)
 tweet = re.sub(r"Crash:", "Crash :", tweet)
 tweet = re.sub(r"casualties.:", "casualties.:", tweet)
 tweet = re.sub(r"Afghanistan:", "Afghanistan :", tweet)
 tweet = re.sub(r"Day:", "Day :", tweet)
 tweet = re.sub(r"AVERTED:", "AVERTED :", tweet)
 tweet = re.sub(r"sitting:", "sitting :", tweet)
 tweet = re.sub(r"Multiplayer:", "Multiplayer :", tweet)
 tweet = re.sub(r"Kaduna:", "Kaduna :", tweet)
 tweet = re.sub(r"favorite:", "favorite :", tweet)
 tweet = re.sub(r"home:", "home :", tweet)
 tweet = re.sub(r"just:", "just :", tweet)
 tweet = re.sub(r"Collision-1141", "Collision - 1141", tweet)
 tweet = re.sub(r"County:", "County :", tweet)
 tweet = re.sub(r"Duty:", "Duty :", tweet)
 tweet = re.sub(r"page:", "page :", tweet)
 tweet = re.sub(r"Attack:", "Attack :", tweet)
 tweet = re.sub(r"Minecraft:", "Minecraft :", tweet)
 tweet = re.sub(r"wounds;", "wounds ;", tweet)
 tweet = re.sub(r"Shots:", "Shots :", tweet)
 tweet = re.sub(r"shots:", "shots :", tweet)
 tweet = re.sub(r"Gunfire:", "Gunfire :", tweet)
 tweet = re.sub(r"hike:", "hike :", tweet)
 tweet = re.sub(r"Email:", "Email :", tweet)
 tweet = re.sub(r"System:", "System :", tweet)
 tweet = re.sub(r"Radio:", "Radio :", tweet)
 tweet = re.sub(r"King:", "King :", tweet)
 tweet = re.sub(r"upheaval:", "upheaval :", tweet)
 tweet = re.sub(r"tragedy;", "tragedy ;", tweet)
 tweet = re.sub(r"HERE:", "HERE :", tweet)
 tweet = re.sub(r"terrorism:", "terrorism :", tweet)
 tweet = re.sub(r"police:", "police :", tweet)
 tweet = re.sub(r"Mosque:", "Mosque :", tweet)
 tweet = re.sub(r"Rightways:", "Rightways :", tweet)
 tweet = re.sub(r"Brooklyn:", "Brooklyn :", tweet)
 tweet = re.sub(r"Arrived:", "Arrived :", tweet)
 tweet = re.sub(r"Home:", "Home :", tweet)
 tweet = re.sub(r"Earth:", "Earth :", tweet)
 tweet = re.sub(r"three:", "three :", tweet)
 tweet = re.sub(r"IranDeal", "Iran Deal", tweet)
 tweet = re.sub(r"ArianaGrande", "Ariana Grande", tweet)
 tweet = re.sub(r"camilacabello97", "camila cabello", tweet)
 tweet = re.sub(r"RondaRousey", "Ronda Rousey", tweet)
 tweet = re.sub(r"MTVHottest", "MTV Hottest", tweet)
 tweet = re.sub(r"TrapMusic", "Trap Music", tweet)
 tweet = re.sub(r"ProphetMuhammad", "Prophet Muhammad", tweet)
 tweet = re.sub(r"PantherAttack", "Panther Attack", tweet)
 tweet = re.sub(r"StrategicPatience", "Strategic Patience", tweet)
 tweet = re.sub(r"socialnews", "social news", tweet)
 tweet = re.sub(r"NASAHurricane", "NASA Hurricane", tweet)
 tweet = re.sub(r"onlinecommunities", "online communities", tweet)
 tweet = re.sub(r"humanconsumption", "human consumption", tweet)
 tweet = re.sub(r"Typhoon-Devastated", "Typhoon Devastated", tweet)
 tweet = re.sub(r"Meat-Loving", "Meat Loving", tweet)
 tweet = re.sub(r"facialabuse", "facial abuse", tweet)
 tweet = re.sub(r"LakeCounty", "Lake County", tweet)
 tweet = re.sub(r"BeingAuthor", "Being Author", tweet)
 tweet = re.sub(r"withheavenly", "with heavenly", tweet)
 tweet = re.sub(r"thankU", "thank you", tweet)
 tweet = re.sub(r"iTunesMusic", "iTunes Music", tweet)
 tweet = re.sub(r"OffensiveContent", "Offensive Content", tweet)
 tweet = re.sub(r"WorstSummerJob", "Worst Summer Job", tweet)
 tweet = re.sub(r"HarryBeCareful", "Harry Be Careful", tweet)
 tweet = re.sub(r"NASASolarSystem", "NASA Solar System", tweet)
 tweet = re.sub(r"animalrescue", "animal rescue", tweet)
 tweet = re.sub(r"KurtSchlichter", "Kurt Schlichter", tweet)
 tweet = re.sub(r"aRmageddon", "armageddon", tweet)
 tweet = re.sub(r"Throwingknifes", "Throwing knives", tweet)
 tweet = re.sub(r"GodsLove", "God's Love", tweet)
 tweet = re.sub(r"bookboost", "book boost", tweet)
 tweet = re.sub(r"ibooklove", "I book love", tweet)
 tweet = re.sub(r"NestleIndia", "Nestle India", tweet)
 tweet = re.sub(r"realDonaldTrump", "Donald Trump", tweet)
 tweet = re.sub(r"DavidVonderhaar", "David Vonderhaar", tweet)
 tweet = re.sub(r"CecilTheLion", "Cecil The Lion", tweet)
 tweet = re.sub(r"weathernetwork", "weather network", tweet)
 tweet = re.sub(r"withBioterrorism&use", "with Bioterrorism & use", tweet)
 tweet = re.sub(r"Hostage&2", "Hostage & 2", tweet)
 tweet = re.sub(r"GOPDebate", "GOP Debate", tweet)
 tweet = re.sub(r"RickPerry", "Rick Perry", tweet)
 tweet = re.sub(r"frontpage", "front page", tweet)
 tweet = re.sub(r"NewsInTweets", "News In Tweets", tweet)
 tweet = re.sub(r"ViralSpell", "Viral Spell", tweet)
 tweet = re.sub(r"til_now", "until now", tweet)
 tweet = re.sub(r"volcanoinRussia", "volcano in Russia", tweet)
 tweet = re.sub(r"ZippedNews", "Zipped News", tweet)
 tweet = re.sub(r"MicheleBachman", "Michele Bachman", tweet)
 tweet = re.sub(r"53inch", "53 inch", tweet)
 tweet = re.sub(r"KerrickTrial", "Kerrick Trial", tweet)
 tweet = re.sub(r"abstorm", "Alberta Storm", tweet)
 tweet = re.sub(r"Beyhive", "Beyonce hive", tweet)
 tweet = re.sub(r"IDFire", "Idaho Fire", tweet)
 tweet = re.sub(r"DETECTADO", "Detected", tweet)
 tweet = re.sub(r"RockyFire", "Rocky Fire", tweet)
 tweet = re.sub(r"Listen/Buy", "Listen / Buy", tweet)
 tweet = re.sub(r"NickCannon", "Nick Cannon", tweet)
 tweet = re.sub(r"FaroeIslands", "Faroe Islands", tweet)
 tweet = re.sub(r"yycstorm", "Calgary Storm", tweet)
 tweet = re.sub(r"IDPs:", "Internally Displaced People :", tweet)
 tweet = re.sub(r"ArtistsUnited", "Artists United", tweet)
 tweet = re.sub(r"ClaytonBryant", "Clayton Bryant", tweet)
 tweet = re.sub(r"jimmyfallon", "jimmy fallon", tweet)
 words = [x.lstrip().rstrip() for x in tweet.split() ]
words = [x for x in words if x and x not in eng_stopwords]
if not words:
return ""
words = ['user' if x.startswith('@')else x for x in words]
tweet = " ".join(words)
 tweet = re.sub(r"[,.;@
return tweet<feature_engineering>
!pip install tfa-nightly
train_pd['combined_filtered_data'] = train_pd['keyword'] + ' ' + train_pd['text']
train_pd['combined_filtered_data'] = train_pd['combined_filtered_data'].apply(lambda x: parse_tweet(x))
train_pd.head()<feature_engineering>
!pip install -q pyyaml h5py
test_pd['combined_filtered_data'] = test_pd['keyword'] + ' ' + test_pd['text']
test_pd['combined_filtered_data'] = test_pd['combined_filtered_data'].apply(lambda x: parse_tweet(x))
test_pd.head()<data_type_conversions>
import tensorflow as tf
from kaggle_datasets import KaggleDatasets
from matplotlib import pyplot as plt
import numpy as np
import random
import tensorflow_addons as tfa
test_pd = test_pd.fillna(value={'combined_filtered_data': ''} )<define_variables>
random.seed(1 )
max_len = max(map(len, train_pd['combined_filtered_data']))
max_len = max(max_len, max(map(len, test_pd['combined_filtered_data'])))
 print(max_len )<define_variables>
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
embedding_dim = 300
trunc_type='post'
padding_type='post'
oov_tok = "<OOV>"<train_model>
AUTO = tf.data.experimental.AUTOTUNE
ignore_order = tf.data.Options()
ignore_order.experimental_deterministic = False
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64)}
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
IMAGE_SIZE = [512,512]
EPOCHS = 20
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
tokenizer = Tokenizer(oov_token=oov_tok)
 tokenizer.fit_on_texts(train_pd['combined_filtered_data'])
 tokenizer.fit_on_texts(test_pd['combined_filtered_data'])
 word_index = tokenizer.word_index
vocab_size = len(word_index )<string_transform>
training_data = tf.io.gfile.glob(GCS_DS_PATH + "/tfrecords-jpeg-512x512/train/*.tfrec")
 validation_data = tf.io.gfile.glob(GCS_DS_PATH + "/tfrecords-jpeg-512x512/val/*.tfrec")
 testing_data = tf.io.gfile.glob(GCS_DS_PATH + "/tfrecords-jpeg-512x512/test/*.tfrec")
 NUM_CLASSES = 104
NUM_TRAINING_IMAGES = 12753
NUM_TEST_IMAGES = 7382
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
X = tokenizer.texts_to_sequences(train_pd['combined_filtered_data'])
 X_padded = pad_sequences(X, maxlen=max_len, padding=padding_type, truncating=trunc_type )<prepare_x_and_y>
def augment_data(image , label):
check = random.randint(0,9)
 if check == 1:
image = flip(image)
 elif check == 2:
image = color(image)
 elif check == 3:
image = rotate(image)
 elif check == 4:
image = shear(image)
 elif check == 5:
image = random_all(image)
 return image , label
split = int(1.0 * len(X_padded))
trainX, trainY = X_padded[:split], train_pd['target'][:split]
valX, valY = X_padded[split:], train_pd['target'][split:]<string_transform>
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
testX = tokenizer.texts_to_sequences(test_pd['combined_filtered_data'])
 testX = pad_sequences(testX, maxlen=max_len, padding=padding_type, truncating=trunc_type )<load_pretrained>
def read_labeled_tfrecord(record):
record = tf.io.parse_single_example(record , LABELED_TFREC_FORMAT)
 image = decode_image(record['image'])
 label = tf.cast(record['class'] , tf.int32)
 return image , label
embeddings_glove = np.load('.. /input/pickled-glove840b300d-for-10sec-loading/glove.840B.300d.pkl', allow_pickle=True )<categorify>
def read_unlabeled_tfrecord(record):
record = tf.io.parse_single_example(record , UNLABELED_TFREC_FORMAT)
 image = decode_image(record['image'])
 id_num = record['id']
return image , id_num
embedding_matrix = np.zeros(( vocab_size + 1, embedding_dim))
for word, i in word_index.items() :
embedding_vector = embeddings_glove.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector<normalization>
def load_dataset(filenames , labeled=True , ordered = False):
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord)
 return dataset
class F1Score(tf.keras.metrics.Metric):
def __init__(self, name='f1_score', **kwargs):
super(F1Score, self ).__init__(name=name, **kwargs)
 self.p = tf.keras.metrics.Precision()
self.r = tf.keras.metrics.Recall()
def update_state(self, *args, **kwargs):
self.p.update_state(*args, **kwargs)
 self.r.update_state(*args, **kwargs)
 def reset_states(self):
self.p.reset_states()
self.r.reset_states()
def result(self):
p_res, r_res = self.p.result() , self.r.result()
return(2 * p_res * r_res)/(p_res + r_res )<choose_model_class>
def load_augmented_dataset(filenames , labeled=True , ordered = False):
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord)
 dataset = dataset.map(augment_data)
 return dataset
def create_model() :
tf.keras.backend.clear_session()
model = tf.keras.Sequential([
tf.keras.layers.Embedding(
 vocab_size + 1, embedding_dim,
weights=[embedding_matrix], input_length=max_len,
trainable=False 
),
tf.keras.layers.Bidirectional(
 tf.keras.layers.LSTM(300)
),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(30, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(
 loss='binary_crossentropy',
optimizer=tf.keras.optimizers.Adam() ,
metrics=[F1Score() ] 
)
 return model<feature_engineering>
def get_training_dataset() :
dataset = load_dataset(training_data , labeled = True , ordered = False)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 return dataset
final_predictions = np.average(predictions, axis=0)
 test_pd['target'] = final_predictions
test_pd['target'] = test_pd['target'].apply(lambda x: 1 if x >= 0.5 else 0 )<save_to_csv>
def get_augmented_dataset() :
dataset = load_augmented_dataset(training_data , labeled = True , ordered = False)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 return dataset
test_pd.to_csv('/kaggle/working/submission.csv', columns=['id', 'target'], index=False )<import_modules>
def get_validation_dataset() :
dataset = load_dataset(validation_data , labeled = True , ordered = False)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
return dataset
<install_modules>
def get_test_dataset(ordered = False):
dataset = load_dataset(testing_data , labeled = False , ordered = ordered)
 dataset = dataset.batch(BATCH_SIZE)
 return dataset
<import_modules>
training_dataset = get_training_dataset()
validation_dataset = get_validation_dataset()
<import_modules>
!pip install efficientnet
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import make_scorer, accuracy_score
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler<load_from_csv>
import efficientnet.tfkeras as efn
df = pd.read_csv('.. /input/train.csv', header = 0, dtype={'Age': np.float64})
 test = pd.read_csv('.. /input/test.csv' , header = 0, dtype={'Age': np.float64} )<groupby>
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss' , patience = 4)
 learning_rate_start = 0.00001
learning_rate_max = 0.00005 * strategy.num_replicas_in_sync
learning_rate_min = 0.0001
learning_rate_boost_epochs = 3
learning_rate_sustain_epochs = 0
learning_rate_decay = 0.9
def learning_rate_schedule(epoch):
if epoch < learning_rate_boost_epochs:
lr =(learning_rate_max - learning_rate_start)/ learning_rate_boost_epochs * epoch + learning_rate_start
elif epoch < learning_rate_boost_epochs + learning_rate_sustain_epochs:
lr = learning_rate_max
else:
lr =(learning_rate_max - learning_rate_min)* learning_rate_decay **(epoch - learning_rate_boost_epochs - learning_rate_sustain_epochs)+ learning_rate_min
return lr
learning_rate_callback = tf.keras.callbacks.LearningRateScheduler(learning_rate_schedule , verbose = True)
df.groupby(by=['Pclass'])['Survived'].agg(['mean','count'] )<set_options>
def create_model() :
with strategy.scope() :
input_layer = tf.keras.layers.Input(shape =(*IMAGE_SIZE,3))
pretrained_model = efn.EfficientNetB7(include_top = False , weights = 'noisy-student' , input_shape =(*IMAGE_SIZE,3), input_tensor = input_layer , pooling='avg')
 for layer in pretrained_model.layers:
layer.trainable = True
X = tf.keras.layers.Dropout(0.2 )(pretrained_model.layers[-1].output)
 X = tf.keras.layers.Dense(NUM_CLASSES , activation = 'softmax' , dtype= 'float32' )(X)
 model = tf.keras.Model(inputs = input_layer , outputs = X)
 optimizer = tf.keras.optimizers.Adam(learning_rate=0.001,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-07,
amsgrad=False)
 loss = tf.keras.losses.SparseCategoricalCrossentropy()
model.compile(loss = loss , optimizer = optimizer , metrics=['sparse_categorical_accuracy'])
 return model
Image(url= "https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/2bc37b51-c9e4-402e-938e-70d3145815f2/d787jna-1b3767d2-f297-4b73-a874-7cfa6d1e8a69.png/v1/fill/w_1600,h_460,q_80,strp/r_m_s__titanic_class_system_by_monroegerman_d787jna-fullview.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9NDYwIiwicGF0aCI6IlwvZlwvMmJjMzdiNTEtYzllNC00MDJlLTkzOGUtNzBkMzE0NTgxNWYyXC9kNzg3am5hLTFiMzc2N2QyLWYyOTctNGI3My1hODc0LTdjZmE2ZDFlOGE2OS5wbmciLCJ3aWR0aCI6Ijw9MTYwMCJ9XV0sImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl19.6krQcPQvsfcQ_ZJ_CGvufi9MT-PJkkg1I8-grLy7Hiw" )<groupby>
model.fit(training_dataset , epochs = EPOCHS , validation_data = validation_dataset , steps_per_epoch = STEPS_PER_EPOCH , callbacks = [learning_rate_callback])
sex_survived= df.groupby(by=['Sex','Survived'])['Survived'].agg(['count'] ).reset_index()
sex_survived<set_options>
augmented_dataset = get_augmented_dataset()
def configure_plotly_browser_state() :
display(IPython.core.display.HTML() )<create_dataframe>
model.fit(augmented_dataset , epochs = EPOCHS , validation_data = validation_dataset , steps_per_epoch = STEPS_PER_EPOCH , callbacks = [learning_rate_callback] )
male_survived=pd.DataFrame(df['Age'][(df['Sex']=='male')&(df['Survived']==1)].value_counts().sort_index(ascending=False)).reset_index().rename(columns={'index':'Age','Age':'Number'})
 female_survived=pd.DataFrame(df['Age'][(df['Sex']=='female')&(df['Survived']==1)].value_counts().sort_index(ascending=False)).reset_index().rename(columns={'index':'Age','Age':'Number'})
 male_not_survived=pd.DataFrame(df['Age'][(df['Sex']=='male')&(df['Survived']==0)].value_counts().sort_index(ascending=False)).reset_index().rename(columns={'index':'Age','Age':'Number'})
 female_not_survived=pd.DataFrame(df['Age'][(df['Sex']=='female')&(df['Survived']==0)].value_counts().sort_index(ascending=False)).reset_index().rename(columns={'index':'Age','Age':'Number'})
<define_variables>
test_dataset = get_test_dataset(ordered=True )
df_all = [df,test]<count_missing_values>
print("Predicting")
 test_images_ds = test_dataset.map(lambda image , idnum : image)
 prob = model.predict(test_images_ds)
 pred = np.argmax(prob , axis = -1)
 print(pred )
<feature_engineering><EOS> 
print("Generating Csv")
 test_ids_ds = test_dataset.map(lambda image , idnum : idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv' , np.rec.fromarrays([test_ids , pred]), fmt=['%s' , '%d'] , delimiter=',',header='id,label' , comments='' )