From c61da5435eb6669c829d006c70a32fe8febae7a5 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Wed, 29 Jan 2020 12:03:17 +0530 Subject: Publish deploy 2020-01-29 12:03 --- .../index 2.html | 23 --- posts/2019-12-22-Fake-News-Detector/index 2.html | 173 --------------------- .../index 2.html | 9 -- .../index 2.html | 1 - posts/hello-world/index 2.html | 1 - posts/index 2.html | 1 - 6 files changed, 208 deletions(-) delete mode 100644 posts/2019-12-10-TensorFlow-Model-Prediction/index 2.html delete mode 100644 posts/2019-12-22-Fake-News-Detector/index 2.html delete mode 100644 posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab/index 2.html delete mode 100644 posts/2020-01-19-Connect-To-Bluetooth-Devices-Linux-Terminal/index 2.html delete mode 100644 posts/hello-world/index 2.html delete mode 100644 posts/index 2.html (limited to 'posts') diff --git a/posts/2019-12-10-TensorFlow-Model-Prediction/index 2.html b/posts/2019-12-10-TensorFlow-Model-Prediction/index 2.html deleted file mode 100644 index 3b71ea8..0000000 --- a/posts/2019-12-10-TensorFlow-Model-Prediction/index 2.html +++ /dev/null @@ -1,23 +0,0 @@ -Making Predictions using Image Classifier (TensorFlow) | Navan Chauhan
🕑 1 minute read.

Making Predictions using Image Classifier (TensorFlow)

This was tested on TF 2.x and works as of 2019-12-10

If you want to understand how to make your own custom image classifier, please refer to my previous post.

If you followed my last post, then you created a model which took an image of dimensions 50x50 as an input.

First we import the following if we have not imported these before

import cv2 -import os -
- -

Then we read the file using OpenCV.

image=cv2.imread(imagePath) -
- -

The cv2. imread() function returns a NumPy array representing the image. Therefore, we need to convert it before we can use it.

image_from_array = Image.fromarray(image, 'RGB') -
- -

Then we resize the image

size_image = image_from_array.resize((50,50)) -
- -

After this we create a batch consisting of only one image

p = np.expand_dims(size_image, 0) -
- -

We then convert this uint8 datatype to a float32 datatype

img = tf.cast(p, tf.float32) -
- -

Finally we make the prediction

print(['Infected','Uninfected'][np.argmax(model.predict(img))]) -
- -

Infected

Tagged with:
\ No newline at end of file diff --git a/posts/2019-12-22-Fake-News-Detector/index 2.html b/posts/2019-12-22-Fake-News-Detector/index 2.html deleted file mode 100644 index a29cd6c..0000000 --- a/posts/2019-12-22-Fake-News-Detector/index 2.html +++ /dev/null @@ -1,173 +0,0 @@ -Building a Fake News Detector with Turicreate | Navan Chauhan
🕑 6 minute read.

Building a Fake News Detector with Turicreate

In this tutorial we will build a fake news detecting app from scratch, using Turicreate for the machine learning model and SwiftUI for building the app

Note: These commands are written as if you are running a jupyter notebook.

Building the Machine Learning Model

Data Gathering

To build a classifier, you need a lot of data. George McIntire (GH: @joolsa) has created a wonderful dataset containing the headline, body and wheter it is fake or real. Whenever you are looking for a dataset, always try searching on Kaggle and GitHub before you start building your own

Dependencies

I used a Google Colab instance for training my model. If you also plan on using Google Colab then I reccomend choosing a GPU Instance (It is Free) This allows you to train the model on the GPU. Turicreat is built on top of Apache's MXNet Framework, for us to use GPU we need to install a CUDA compatible MXNet package.

!pip install turicreate -!pip uninstall -y mxnet -!pip install mxnet-cu100==1.4.0.post0 -
- -

If you do not wish to train on GPU or are running it on your computer, you can ignore the last two lines

Downloading the Dataset

!wget -q "https://github.com/joolsa/fake_real_news_dataset/raw/master/fake_or_real_news.csv.zip" -!unzip fake_or_real_news.csv.zip -
- -

Model Creation

import turicreate as tc -tc.config.set_num_gpus(-1) # If you do not wish to use GPUs, set it to 0 -
- -
dataSFrame = tc.SFrame('fake_or_real_news.csv') -
- -

The dataset contains a column named "X1", which is of no use to us. Therefore, we simply drop it

dataSFrame.remove_column('X1') -
- -

Splitting Dataset

train, test = dataSFrame.random_split(.9) -
- -

Training

model = tc.text_classifier.create( - dataset=train, - target='label', - features=['title','text'] -) -
- -
+-----------+----------+-----------+--------------+-------------------+---------------------+ -| Iteration | Passes | Step size | Elapsed Time | Training Accuracy | Validation Accuracy | -+-----------+----------+-----------+--------------+-------------------+---------------------+ -| 0 | 2 | 1.000000 | 1.156349 | 0.889680 | 0.790036 | -| 1 | 4 | 1.000000 | 1.359196 | 0.985952 | 0.918149 | -| 2 | 6 | 0.820091 | 1.557205 | 0.990260 | 0.914591 | -| 3 | 7 | 1.000000 | 1.684872 | 0.998689 | 0.925267 | -| 4 | 8 | 1.000000 | 1.814194 | 0.999063 | 0.925267 | -| 9 | 14 | 1.000000 | 2.507072 | 1.000000 | 0.911032 | -+-----------+----------+-----------+--------------+-------------------+---------------------+ -
- -

Testing the Model

est_predictions = model.predict(test) -accuracy = tc.evaluation.accuracy(test['label'], test_predictions) -print(f'Topic classifier model has a testing accuracy of {accuracy*100}% ', flush=True) -
- -
Topic classifier model has a testing accuracy of 92.3076923076923% -
- -

We have just created our own Fake News Detection Model which has an accuracy of 92%!

example_text = {"title": ["Middling ‘Rise Of Skywalker’ Review Leaves Fan On Fence About Whether To Threaten To Kill Critic"], "text": ["Expressing ambivalence toward the relatively balanced appraisal of the film, Star Wars fan Miles Ariely admitted Thursday that an online publication’s middling review of The Rise Of Skywalker had left him on the fence about whether he would still threaten to kill the critic who wrote it. “I’m really of two minds about this, because on the one hand, he said the new movie fails to live up to the original trilogy, which makes me at least want to throw a brick through his window with a note telling him to watch his back,” said Ariely, confirming he had already drafted an eight-page-long death threat to Stan Corimer of the website Screen-On Time, but had not yet decided whether to post it to the reviewer’s Facebook page. “On the other hand, though, he commended J.J. Abrams’ skillful pacing and faithfulness to George Lucas’ vision, which makes me wonder if I should just call the whole thing off. Now, I really don’t feel like camping outside his house for hours. Maybe I could go with a response that’s somewhere in between, like, threatening to kill his dog but not everyone in his whole family? I don’t know. This is a tough one.” At press time, sources reported that Ariely had resolved to wear his Ewok costume while he murdered the critic in his sleep."]} -example_prediction = model.classify(tc.SFrame(example_text)) -print(example_prediction, flush=True) -
- -
+-------+--------------------+ -| class | probability | -+-------+--------------------+ -| FAKE | 0.9245648658345308 | -+-------+--------------------+ -[1 rows x 2 columns] -
- -

Exporting the Model

model_name = 'FakeNews' -coreml_model_name = model_name + '.mlmodel' -exportedModel = model.export_coreml(coreml_model_name) -
- -

Note: To download files from Google Volab, simply click on the files section in the sidebar, right click on filename and then click on downlaod

Link to Colab Notebook

Building the App using SwiftUI

Initial Setup

First we create a single view app (make sure you check the use SwiftUI button)

Then we copy our .mlmodel file to our project (Just drag and drop the file in the XCode Files Sidebar)

Our ML Model does not take a string directly as an input, rather it takes bag of words as an input. DescriptionThe bag-of-words model is a simplifying representation used in NLP, in this text is represented as a bag of words, without any regatd of grammar or order, but noting multiplicity

We define our bag of words function

func bow(text: String) -> [String: Double] { - var bagOfWords = [String: Double]() - - let tagger = NSLinguisticTagger(tagSchemes: [.tokenType], options: 0) - let range = NSRange(location: 0, length: text.utf16.count) - let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace] - tagger.string = text - - tagger.enumerateTags(in: range, unit: .word, scheme: .tokenType, options: options) { _, tokenRange, _ in - let word = (text as NSString).substring(with: tokenRange) - if bagOfWords[word] != nil { - bagOfWords[word]! += 1 - } else { - bagOfWords[word] = 1 - } - } - - return bagOfWords - } -
- -

We also declare our variables

@State private var title: String = "" -@State private var headline: String = "" -@State private var alertTitle = "" -@State private var alertText = "" -@State private var showingAlert = false -
- -

Finally, we implement a simple function which reads the two text fields, creates their bag of words representation and displays an alert with the appropriate result

Complete Code

import SwiftUI - -struct ContentView: View { - @State private var title: String = "" - @State private var headline: String = "" - - @State private var alertTitle = "" - @State private var alertText = "" - @State private var showingAlert = false - - var body: some View { - NavigationView { - VStack(alignment: .leading) { - Text("Headline").font(.headline) - TextField("Please Enter Headline", text: $title) - .lineLimit(nil) - Text("Body").font(.headline) - TextField("Please Enter the content", text: $headline) - .lineLimit(nil) - } - .navigationBarTitle("Fake News Checker") - .navigationBarItems(trailing: - Button(action: classifyFakeNews) { - Text("Check") - }) - .padding() - .alert(isPresented: $showingAlert){ - Alert(title: Text(alertTitle), message: Text(alertText), dismissButton: .default(Text("OK"))) - } - } - - } - - func classifyFakeNews(){ - let model = FakeNews() - let myTitle = bow(text: title) - let myText = bow(text: headline) - do { - let prediction = try model.prediction(title: myTitle, text: myText) - alertTitle = prediction.label - alertText = "It is likely that this piece of news is \(prediction.label.lowercased())." - print(alertText) - } catch { - alertTitle = "Error" - alertText = "Sorry, could not classify if the input news was fake or not." - } - - showingAlert = true - } - func bow(text: String) -> [String: Double] { - var bagOfWords = [String: Double]() - - let tagger = NSLinguisticTagger(tagSchemes: [.tokenType], options: 0) - let range = NSRange(location: 0, length: text.utf16.count) - let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace] - tagger.string = text - - tagger.enumerateTags(in: range, unit: .word, scheme: .tokenType, options: options) { _, tokenRange, _ in - let word = (text as NSString).substring(with: tokenRange) - if bagOfWords[word] != nil { - bagOfWords[word]! += 1 - } else { - bagOfWords[word] = 1 - } - } - - return bagOfWords - } -} - -struct ContentView_Previews: PreviewProvider { - static var previews: some View { - ContentView() - } -} -
- -
Tagged with:
\ No newline at end of file diff --git a/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab/index 2.html b/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab/index 2.html deleted file mode 100644 index ab122f4..0000000 --- a/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab/index 2.html +++ /dev/null @@ -1,9 +0,0 @@ -Setting up Kaggle to use with Google Colab | Navan Chauhan
🕑 1 minute read.

Setting up Kaggle to use with Google Colab

In order to be able to access Kaggle Datasets, you will need to have an account on Kaggle (which is Free)

Grabbing Our Tokens

Go to Kaggle

Click on your User Profile and Click on My Account

Scroll Down untill you see Create New API Token

This will download your token as a JSON file

Copy the File to the root folder of your Google Drive

Setting up Colab

Mounting Google Drive

import os -from google.colab import drive -drive.mount('/content/drive') -
- -

After this click on the URL in the output section, login and then paste the Auth Code

Configuring Kaggle

os.environ['KAGGLE_CONFIG_DIR'] = "/content/drive/My Drive/" -
- -

Voila! You can now download kaggel datasets

Tagged with:
\ No newline at end of file diff --git a/posts/2020-01-19-Connect-To-Bluetooth-Devices-Linux-Terminal/index 2.html b/posts/2020-01-19-Connect-To-Bluetooth-Devices-Linux-Terminal/index 2.html deleted file mode 100644 index 4f56a6d..0000000 --- a/posts/2020-01-19-Connect-To-Bluetooth-Devices-Linux-Terminal/index 2.html +++ /dev/null @@ -1 +0,0 @@ -How to setup Bluetooth on a Raspberry Pi | Navan Chauhan
🕑 0 minute read.

How to setup Bluetooth on a Raspberry Pi

This was tested on a Raspberry Pi Zero W

Enter in the Bluetooth Mode

pi@raspberrypi:~ $ bluetoothctl

[bluetooth]# agent on

[bluetooth]# default-agent

[bluetooth]# scan on

To Pair

While being in bluetooth mode

[bluetooth]# pair XX:XX:XX:XX:XX:XX

To Exit out of bluetoothctl anytime, just type exit

Tagged with:
\ No newline at end of file diff --git a/posts/hello-world/index 2.html b/posts/hello-world/index 2.html deleted file mode 100644 index e819ba1..0000000 --- a/posts/hello-world/index 2.html +++ /dev/null @@ -1 +0,0 @@ -Hello World | Navan Chauhan
🕑 0 minute read.

Hello World

Why a Hello World post?

Just re-did the entire website using Publish (Publish by John Sundell). So, a new hello world post :)

Tagged with:
\ No newline at end of file diff --git a/posts/index 2.html b/posts/index 2.html deleted file mode 100644 index 9ac470f..0000000 --- a/posts/index 2.html +++ /dev/null @@ -1 +0,0 @@ -Posts | Navan Chauhan

Posts

Tips, tricks and tutorials which I think might be useful.

\ No newline at end of file -- cgit v1.2.3