Transfer learning with TensorFlow Hub: using a single test image? - tensorflow

I have successfully followed this official tutorial on image classification with transfer learning: https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub
My experimental model is now saved and supposed to recognize when it sees a "good" painting. However, I want to test this with an image that the model has not seen before. So far I have only used notebooks where the dataset is already divided into train and test folders. However, this is not the case here.
I assume I need something like
img = tf.keras.preprocessing.image.load_img("/content/mytestimage.jpeg", target_size=(224,224))
among other things; however, for a beginner it would be useful to see an example of this kind of test prediction. So far I have searched without results - if anyone has any advice I'm super happy to hear!

Here's how to do it with mobilenet transfer learning with keras but most of the code should be the same. A full transfer learning tutorial can be found here. I found it very useful.
from PIL import Image
from tensorflow.keras.models import load_model
model = load_model('path/to/model.h5')
img = Image.open(file)
array = np.asarray(img, dtype=np.float32)
arrayexp = np.expand_dims(array, axis=0)
arrayexp = (arrayexp/127)-1 #This is a normalization factor specifically for Mobilenet but I think it's used for many other networks
result = model.predict(arrayexp)
print(np.argmax(result)) #Prints class with highest confidence
print(result[0][np.argmax(result)]) #Prints confidence for the highest

Related

Output mismatch with Landmarks Classification Model North America on TensorFlow Hub

I am trying to load the pretrained model from tensorflow hub (link here: https://tfhub.dev/google/on_device_vision/classifier/landmarks_classifier_north_america_V1/1). When I run inference on a single image, I get an output on length 99424, but the corresponding labelmap is of length 99676. This doesn't make any sense to me as the lengths of the two should be the same length. Because of this error, the model is not classifying accurately whatsoever. Is anyone else having the same error. If not, any guidance would be greatly appreciated.
import tensorflow.compat.v2 as tf
import tensorflow_hub as hub
model = hub.KerasLayer(model_url, output_key='predictions:logits')
landmarks = pd.read_csv(landmark_file)
image = load_image(im_path) # (321, 321, 3) scaled between [0,1]
output = model(image)
prediction = landmarks['name'][int(tf.math.argmax(output, 1))]
Additionally, I get this error when loading the model:
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
I feel like it is a problem with tensorflow hub's model, but not entirely sure
As of 2021-03-18, the inconsistencies in the labelmaps have been fixed. You just need to re-download the labelmap files, which are referenced from the model pages on tfhub.dev. Hope that helps and sorry for the inconvenience!

Develop Question Answer System using BERT

i'm currently developing a Question-Answer system (in Indonesian language) using BERT for my thesis.
The dataset and the questions given are in Indonesian.
The problem is, i'm still not clear on how the step-to-step process to develop the Question-Answer system in BERT.
From what I concluded after reading a number of research journals and papers, the process might be like this:
Prepare main dataset
Load Pre-Train Data
Train the main dataset with the pre-train data (so that it produce "fine-tuned" model)
Cluster the fine-tuned model
Testing (giving questions to the system)
Evaluation
What i want to ask are :
Are those steps correct? Or maybe there any missing step(s)?
Also, if the default pre-train data that BERT provide is in English while my main dataset is in Indonesian, how can i create my own indonesian pre-train data?
Does it really need to perform data/model clustering in BERT?
I appreciate any helpful answer(s).
Thank you very much in advance.
I would take a look at Huggingface's Question & Answer examples. That would at least be a good place to start.
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
text = r"""
🤗 Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides general-purpose
architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet…) for Natural Language Understanding (NLU) and Natural
Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between
TensorFlow 2.0 and PyTorch.
"""
questions = [
"How many pretrained models are available in Transformers?",
"What does Transformers provide?",
"Transformers provides interoperability between which frameworks?",
]
for question in questions:
inputs = tokenizer.encode_plus(question, text, add_special_tokens=True, return_tensors="pt")
input_ids = inputs["input_ids"].tolist()[0]
text_tokens = tokenizer.convert_ids_to_tokens(input_ids)
answer_start_scores, answer_end_scores = model(**inputs)
answer_start = torch.argmax(
answer_start_scores
) # Get the most likely beginning of answer with the argmax of the score
answer_end = torch.argmax(answer_end_scores) + 1 # Get the most likely end of answer with the argmax of the score
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
print(f"Question: {question}")
print(f"Answer: {answer}\n")

Tensorflow image classification example

This is my first time doing image classification, I followed this tutorial:
https://www.tensorflow.org/tutorials/images/classification
I'm wondering, how do I take that model, and actually use it to make predictions?
I would just to put one image into the model, and would ideally like to get a prediction % of whether it thinks its a dog or a cat.
I saved the model using:
model.save(my_model.h5)
But am really lost at the next steps.
There's another Tensorflow tutorial which uses model.predict() specifically: Basic classification: Classify images of clothing
Not sure if my code is correct all the way but I tried to extend the prediction part of the cats/dogs tutorial using model.predict_generator() though I can't seem to entirely understand the results I get. Adapted code from this second tutorial: Tutorial on using Keras flow_from_directory and generators
# Preparing the testing dataset
test_dir = os.path.join(os.getcwd(), 'cat_dog_testing') # directory with test images
test_image_generator = ImageDataGenerator(rescale=1./255) # rescaling pixels 0 to 1
test_generator = test_image_generator.flow_from_directory(batch_size=6,
directory=test_dir,
shuffle=False,
target_size=(IMG_HEIGHT,IMG_WIDTH),
class_mode=None)
STEP_SIZE_TEST=test_generator.n//test_generator.batch_size
test_generator.reset()
pred=model_new.predict_generator(test_generator, steps=STEP_SIZE_TEST, verbose=1)
I built a tensorflow image classification workflow so that you can both train and classify images with no code. It's on FlyteHub if you want to see it
https://flytehub.org/trainandclassifyimages
Happy to collaborate if you have improvements you want to make to the codebase :)

Updating a BERT model through Huggingface transformers

I am attempting to update the pre-trained BERT model using an in house corpus. I have looked at the Huggingface transformer docs and I am a little stuck as you will see below.My goal is to compute simple similarities between sentences using the cosine distance but I need to update the pre-trained model for my specific use case.
If you look at the code below, which is precisely from the Huggingface docs. I am attempting to "retrain" or update the model and I assumed that special_token_1 and special_token_2 represent "new sentences" from my "in house" data or corpus. Is this correct? In summary, I like the already pre-trained BERT model but I would like to update it or retrain it using another in house dataset. Any leads will be appreciated.
import tensorflow as tf
import tensorflow_datasets
from transformers import *
model = BertModel.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
SPECIAL_TOKEN_1="dogs are very cute"
SPECIAL_TOKEN_2="dogs are cute but i like cats better and my
brother thinks they are more cute"
tokenizer.add_tokens([SPECIAL_TOKEN_1, SPECIAL_TOKEN_2])
model.resize_token_embeddings(len(tokenizer))
#Train our model
model.train()
model.eval()
BERT is pre-trained on 2 tasks: masked language modeling (MLM) and next sentence prediction (NSP). The most important of those two is MLM (it turns out that the next sentence prediction task is not really that helpful for the model's language understanding capabilities - RoBERTa for example is only pre-trained on MLM).
If you want to further train the model on your own dataset, you can do so by using BERTForMaskedLM in the Transformers repository. This is BERT with a language modeling head on top, which allows you to perform masked language modeling (i.e. predicting masked tokens) on your own dataset. Here's how to use it:
from transformers import BertTokenizer, BertForMaskedLM
import torch
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForMaskedLM.from_pretrained('bert-base-uncased', return_dict=True)
inputs = tokenizer("The capital of France is [MASK].", return_tensors="pt")
labels = tokenizer("The capital of France is Paris.", return_tensors="pt")["input_ids"]
outputs = model(**inputs, labels=labels)
loss = outputs.loss
logits = outputs.logits
You can update the weights of BertForMaskedLM using loss.backward(), which is the main way of training PyTorch models. If you don't want to do this yourself, the Transformers library also provides a Python script which allows you perform MLM really quickly on your own dataset. See here (section "RoBERTa/BERT/DistilBERT and masked language modeling"). You just need to provide a training and test file.
You don't need to add any special tokens. Examples of special tokens are [CLS] and [SEP], which are used for sequence classification and question answering tasks (among others). These are added by the tokenizer automatically. How do I know this? Because BertTokenizer inherits from PretrainedTokenizer, and if you take a look at the documentation of its __call__ method here, you can see that the add_special_tokens parameter defaults to True.

how can i use tqdm to visualize the progress of training steps using tf.data.Dataset api?

I want to use tqdm to visualize my cnn network training steps.
How can I implement tqdm with tf.data.Dataset() api?
Can u show me a sample code? thx!
NOTE: although this is an obviously flagged question with mal practise, I still think it is a valid question (I had myself) and post a solution.
A possibility is to obtain the cardinality of your dataset previously and use it as #mr.melon states in tqdm.
cardinality = np.sum([1 for i in dataset.batch(batch_size)])
where dataset is of class tf.data.Dataset and you haven't done the full preparation pipeline (I refere to interleaving, shuffling, batch and prefect and their kind).
Then you can
for input, label in tqdm(dataset, total=cardinality):
...
It's pretty easy:
Derive the number of samples in your dataset,
Then, convert the number into some iterable python structure.
for _ in tqdm(iterable=xxx, total=num_samples):
batch_data = sess.run(ele_derived_from_tf_dataset)