SlideShare a Scribd company logo
1 of 11
Download to read offline
I have tried running this code below, and it is working, but the accuracy is less than expected.
import os
import numpy as np
import pandas as pd
import keras
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import load_img
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import random
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, MaxPool2D , Dropout, Flatten, Dense,
Activation, BatchNormalization
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from sklearn.utils import class_weight
import keras,os
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D , Flatten
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, EarlyStopping
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score,
cohen_kappa_score, roc_auc_score
print(os.listdir("D:RansomSecondApproachRansomware_Detection_using
_CNNMixImages"))
# Define Constants
FAST_RUN = False
IMAGE_WIDTH=256 # 150 accept maybe 256
IMAGE_HEIGHT=256 # maybe 256
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)
IMAGE_CHANNELS=3 # maybe not need
physical_devices = tf.config.experimental.list_physical_devices('GPU')
print(physical_devices)
if physical_devices:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
# Prepare Traning Data
filenames = os.listdir("D:RansomSecondApproachRansomware_Detection_using
_CNNMixImages")
categories = []
for filename in filenames:
category = filename.split('l')[0]
if category == 'image_benign_':
categories.append(0)
else:
categories.append(1)
df = pd.DataFrame({
'filename': filenames,
'category': categories
})
print(df.head())
print(df.tail())
# in collab it will work
df['category'].value_counts().plot.bar()
model = Sequential()
model.add(Conv2D(16, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH,
IMAGE_HEIGHT, IMAGE_CHANNELS)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax')) # 2 because we have cat and dog classes
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Callbacks
## Early Stop To prevent over fitting we will stop the learning after 10 epochs and val_loss value
not decreased
earlystop = EarlyStopping(patience=10)
# Learning Rate Reduction
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=2,
verbose=1,
factor=0.5,
min_lr=0.00001)
callbacks = [earlystop, learning_rate_reduction]
#Prepare data
df["category"] = df["category"].replace({0: 'benign', 1: 'malware'})
train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)
train_df = train_df.reset_index(drop=True)
validate_df = validate_df.reset_index(drop=True)
total_train = train_df.shape[0]
total_validate = validate_df.shape[0]
batch_size= 15 # defualt 15
# Traning Generator
# Defualt
train_datagen = ImageDataGenerator(
rotation_range=15,
rescale=1./255,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
width_shift_range=0.1,
height_shift_range=0.1
)
###############################################
# Augmenting the training data
train_generator = train_datagen.flow_from_dataframe(
train_df,
"D:RansomSecondApproachRansomware_Detection_using _CNNMixImages",
x_col='filename',
y_col='category',
target_size=IMAGE_SIZE,
class_mode='categorical',
batch_size=batch_size
)
# Validation Generator
validation_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = validation_datagen.flow_from_dataframe(
validate_df,
"D:RansomSecondApproachRansomware_Detection_using _CNNMixImages",
x_col='filename',
y_col='category',
target_size=IMAGE_SIZE,
class_mode='categorical',
batch_size=batch_size
)
# See how our generator work
example_df = train_df.sample(n=1).reset_index(drop=True)
example_generator = train_datagen.flow_from_dataframe(
example_df,
"D:RansomSecondApproachRansomware_Detection_using _CNNMixImages",
x_col='filename',
y_col='category',
target_size=IMAGE_SIZE,
class_mode='categorical'
)
# in collab it will work
plt.figure(figsize=(12, 12))
for i in range(0, 15):
plt.subplot(5, 3, i+1)
for X_batch, Y_batch in example_generator:
image = X_batch[0]
plt.imshow(image)
break
plt.tight_layout()
plt.show()
# compile
epochs = 1 # if FAST_RUN else 50
history = model.fit(
train_generator,
epochs=epochs,
validation_data=validation_generator,
validation_steps=total_validate//batch_size,
steps_per_epoch=total_train//batch_size,
# class_weight=class_weights, # add
# shuffle=True,
callbacks=callbacks
)
model.save_weights("model.h5")
# Virtualize Training
##in collab
# fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))
# ax1.plot(history.history['loss'], color='b', label="Training loss")
# ax1.plot(history.history['val_loss'], color='r', label="validation loss")
# ax1.set_xticks(np.arange(1, epochs, 1))
# ax1.set_yticks(np.arange(0, 1, 0.1))
# ax2.plot(history.history['acc'], color='b', label="Training accuracy")
# ax2.plot(history.history['val_acc'], color='r',label="Validation accuracy")
# ax2.set_xticks(np.arange(1, epochs, 1))
# legend = plt.legend(loc='best', shadow=True)
# plt.tight_layout()
# plt.show()
# Prepare Testing Data
test_filenames = os.listdir("D:RansomSecondApproachRansomware_Detection_using
_CNNMixImages")
test_df = pd.DataFrame({
'filename': test_filenames
})
nb_samples = test_df.shape[0]
# Create Testing Generator
# output Found 12500 images in kaggle.
test_gen = ImageDataGenerator(rescale=1./255)
test_generator = test_gen.flow_from_dataframe(
test_df,
"D:RansomSecondApproachRansomware_Detection_using _CNNMixImages",
x_col='filename',
y_col=None,
class_mode=None,
target_size=IMAGE_SIZE,
batch_size=batch_size,
shuffle=False
)
# Predict
predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))
test_df['category'] = np.argmax(predict, axis=-1)
label_map = dict((v,k) for k,v in train_generator.class_indices.items())
test_df['category'] = test_df['category'].replace(label_map)
test_df['category'] = test_df['category'].replace({ 0: 'benign', 1: 'malware' })
# Virtaulize Result
# Compute the average training and validation accuracy
avg_train_acc = np.mean(history.history['accuracy'])
avg_val_acc = np.mean(history.history['val_accuracy'])
# Print the average training and validation accuracy
print("Average training accuracy:", avg_train_acc)
print("Average validation accuracy:", avg_val_acc)
#in collab
test_df['category'].value_counts().plot.bar()
# See predicted result with images
# in collab
sample_test = test_df.head(18)
sample_test.head()
plt.figure(figsize=(12, 24))
for index, row in sample_test.iterrows():
filename = row['filename']
category = row['category']
img = load_img("D:RansomSecondApproachRansomware_Detection_using
_CNNMixImages" +filename, target_size=IMAGE_SIZE)
plt.subplot(6, 3, index+1)
plt.imshow(img)
plt.xlabel(filename + '(' + "{}".format(category) + ')' )
plt.tight_layout()
plt.show()
# Submission
submission_df = test_df.copy()
submission_df['id'] = submission_df['filename'].str.split('.').str[0]
submission_df['label'] = submission_df['category']
submission_df.drop(['filename', 'category'], axis=1, inplace=True)
submission_df.to_csv('submission.csv', index=False)
First, please add the output of the F1 score, Precision, ROC AUC, and Cohen kappa to the
code.
Second, please add or modify the code for a higher result.
I need coding only.
Thank you in advance.
I have tried running this code below- and it is working- but the accur.pdf

More Related Content

Similar to I have tried running this code below- and it is working- but the accur.pdf

Need helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdfNeed helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdf
actexerode
 
Python program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docxPython program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docx
LukeQVdGrantg
 
Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
acteleshoppe
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
ssuserb4d806
 
Gearmanpresentation 110308165409-phpapp01
Gearmanpresentation 110308165409-phpapp01Gearmanpresentation 110308165409-phpapp01
Gearmanpresentation 110308165409-phpapp01
longtuan
 

Similar to I have tried running this code below- and it is working- but the accur.pdf (20)

29-kashyap-mask-detaction.pptx
29-kashyap-mask-detaction.pptx29-kashyap-mask-detaction.pptx
29-kashyap-mask-detaction.pptx
 
Need helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdfNeed helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdf
 
Image classification using cnn
Image classification using cnnImage classification using cnn
Image classification using cnn
 
Viktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning ServiceViktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning Service
 
Python program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docxPython program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docx
 
Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
 
CE344L-200365-Lab6.pdf
CE344L-200365-Lab6.pdfCE344L-200365-Lab6.pdf
CE344L-200365-Lab6.pdf
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
 
Gearmanpresentation 110308165409-phpapp01
Gearmanpresentation 110308165409-phpapp01Gearmanpresentation 110308165409-phpapp01
Gearmanpresentation 110308165409-phpapp01
 
Competition 1 (blog 1)
Competition 1 (blog 1)Competition 1 (blog 1)
Competition 1 (blog 1)
 
Machine Learning - Introduction
Machine Learning - IntroductionMachine Learning - Introduction
Machine Learning - Introduction
 
GANS Project for Image idetification.pdf
GANS Project for Image idetification.pdfGANS Project for Image idetification.pdf
GANS Project for Image idetification.pdf
 
Learning Predictive Modeling with TSA and Kaggle
Learning Predictive Modeling with TSA and KaggleLearning Predictive Modeling with TSA and Kaggle
Learning Predictive Modeling with TSA and Kaggle
 
Tutorial: Image Generation and Image-to-Image Translation using GAN
Tutorial: Image Generation and Image-to-Image Translation using GANTutorial: Image Generation and Image-to-Image Translation using GAN
Tutorial: Image Generation and Image-to-Image Translation using GAN
 
Deep learning approach for intelligent intrusion detection system
Deep learning approach for intelligent intrusion detection systemDeep learning approach for intelligent intrusion detection system
Deep learning approach for intelligent intrusion detection system
 
C3 w2
C3 w2C3 w2
C3 w2
 
Unsupervised Aspect Based Sentiment Analysis at Scale
Unsupervised Aspect Based Sentiment Analysis at ScaleUnsupervised Aspect Based Sentiment Analysis at Scale
Unsupervised Aspect Based Sentiment Analysis at Scale
 
Training course lect2
Training course lect2Training course lect2
Training course lect2
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
 
Jeroen Vloothuis Bend Kss To Your Will
Jeroen Vloothuis   Bend Kss To Your WillJeroen Vloothuis   Bend Kss To Your Will
Jeroen Vloothuis Bend Kss To Your Will
 

More from GordonF2XPatersonh

In 2020- the world was made aware of the highly contagious and occasio.pdf
In 2020- the world was made aware of the highly contagious and occasio.pdfIn 2020- the world was made aware of the highly contagious and occasio.pdf
In 2020- the world was made aware of the highly contagious and occasio.pdf
GordonF2XPatersonh
 
import java-util-ArrayList- public class Bus { private String na.pdf
import java-util-ArrayList-   public class Bus {     private String na.pdfimport java-util-ArrayList-   public class Bus {     private String na.pdf
import java-util-ArrayList- public class Bus { private String na.pdf
GordonF2XPatersonh
 

More from GordonF2XPatersonh (20)

import java-lang-Comparable- -- new Main-String-() public class Main-.pdf
import java-lang-Comparable-  -- new Main-String-() public class Main-.pdfimport java-lang-Comparable-  -- new Main-String-() public class Main-.pdf
import java-lang-Comparable- -- new Main-String-() public class Main-.pdf
 
In 2022- Jacques paid the following interest- Interest on home mortgag.pdf
In 2022- Jacques paid the following interest- Interest on home mortgag.pdfIn 2022- Jacques paid the following interest- Interest on home mortgag.pdf
In 2022- Jacques paid the following interest- Interest on home mortgag.pdf
 
In 2020- the world was made aware of the highly contagious and occasio.pdf
In 2020- the world was made aware of the highly contagious and occasio.pdfIn 2020- the world was made aware of the highly contagious and occasio.pdf
In 2020- the world was made aware of the highly contagious and occasio.pdf
 
Implement an extended version of the Caesar cipher that users the char.pdf
Implement an extended version of the Caesar cipher that users the char.pdfImplement an extended version of the Caesar cipher that users the char.pdf
Implement an extended version of the Caesar cipher that users the char.pdf
 
Implement FIFO queue using two stacks in Python- Methods should have O.pdf
Implement FIFO queue using two stacks in Python- Methods should have O.pdfImplement FIFO queue using two stacks in Python- Methods should have O.pdf
Implement FIFO queue using two stacks in Python- Methods should have O.pdf
 
import java-util-ArrayList- public class Bus { private String na.pdf
import java-util-ArrayList-   public class Bus {     private String na.pdfimport java-util-ArrayList-   public class Bus {     private String na.pdf
import java-util-ArrayList- public class Bus { private String na.pdf
 
Imagine a natural area near your hometown that has been identified as.pdf
Imagine a natural area near your hometown that has been identified as.pdfImagine a natural area near your hometown that has been identified as.pdf
Imagine a natural area near your hometown that has been identified as.pdf
 
If you wish to estimate a population mean with a sampling distribution.pdf
If you wish to estimate a population mean with a sampling distribution.pdfIf you wish to estimate a population mean with a sampling distribution.pdf
If you wish to estimate a population mean with a sampling distribution.pdf
 
igate Search Project Run Window Help D eclipse-worksoace - Names javar.pdf
igate Search Project Run Window Help D eclipse-worksoace - Names javar.pdfigate Search Project Run Window Help D eclipse-worksoace - Names javar.pdf
igate Search Project Run Window Help D eclipse-worksoace - Names javar.pdf
 
If you have a dataset with n-42- mean is 50- the standard deviation is.pdf
If you have a dataset with n-42- mean is 50- the standard deviation is.pdfIf you have a dataset with n-42- mean is 50- the standard deviation is.pdf
If you have a dataset with n-42- mean is 50- the standard deviation is.pdf
 
If you combine cooking oil (a lipid) with water in a measuring cup- th.pdf
If you combine cooking oil (a lipid) with water in a measuring cup- th.pdfIf you combine cooking oil (a lipid) with water in a measuring cup- th.pdf
If you combine cooking oil (a lipid) with water in a measuring cup- th.pdf
 
If X has a binomial random variable with probability distribution f(x).pdf
If X has a binomial random variable with probability distribution f(x).pdfIf X has a binomial random variable with probability distribution f(x).pdf
If X has a binomial random variable with probability distribution f(x).pdf
 
If we look at the diagram below we notice that there are soveral chara.pdf
If we look at the diagram below we notice that there are soveral chara.pdfIf we look at the diagram below we notice that there are soveral chara.pdf
If we look at the diagram below we notice that there are soveral chara.pdf
 
If total assets decrease- then which of the following statements is tr.pdf
If total assets decrease- then which of the following statements is tr.pdfIf total assets decrease- then which of the following statements is tr.pdf
If total assets decrease- then which of the following statements is tr.pdf
 
If two different accountants were to estimate the percentage of custom.pdf
If two different accountants were to estimate the percentage of custom.pdfIf two different accountants were to estimate the percentage of custom.pdf
If two different accountants were to estimate the percentage of custom.pdf
 
If the radius of the Earth were to increase by a factor of 4 while its.pdf
If the radius of the Earth were to increase by a factor of 4 while its.pdfIf the radius of the Earth were to increase by a factor of 4 while its.pdf
If the radius of the Earth were to increase by a factor of 4 while its.pdf
 
If the government suddenly decided to include the non-civilian employe.pdf
If the government suddenly decided to include the non-civilian employe.pdfIf the government suddenly decided to include the non-civilian employe.pdf
If the government suddenly decided to include the non-civilian employe.pdf
 
If the Earth's P-wave velocities were faster than they actually are- t.pdf
If the Earth's P-wave velocities were faster than they actually are- t.pdfIf the Earth's P-wave velocities were faster than they actually are- t.pdf
If the Earth's P-wave velocities were faster than they actually are- t.pdf
 
If the Earth was made-up entirely of crustal rocks- then what would th.pdf
If the Earth was made-up entirely of crustal rocks- then what would th.pdfIf the Earth was made-up entirely of crustal rocks- then what would th.pdf
If the Earth was made-up entirely of crustal rocks- then what would th.pdf
 
If the anhydrous compound (AC) in the preceding problem has a molar ma.pdf
If the anhydrous compound (AC) in the preceding problem has a molar ma.pdfIf the anhydrous compound (AC) in the preceding problem has a molar ma.pdf
If the anhydrous compound (AC) in the preceding problem has a molar ma.pdf
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

I have tried running this code below- and it is working- but the accur.pdf

  • 1. I have tried running this code below, and it is working, but the accuracy is less than expected. import os import numpy as np import pandas as pd import keras import tensorflow as tf from keras.preprocessing.image import ImageDataGenerator from keras.utils import load_img from keras.utils import to_categorical from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import random from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, MaxPool2D , Dropout, Flatten, Dense, Activation, BatchNormalization from keras.callbacks import EarlyStopping, ReduceLROnPlateau from sklearn.utils import class_weight import keras,os from keras.models import Sequential from keras.layers import Dense, Conv2D, MaxPool2D , Flatten from keras.preprocessing.image import ImageDataGenerator import numpy as np from keras.optimizers import Adam from keras.callbacks import ModelCheckpoint, EarlyStopping
  • 2. from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, cohen_kappa_score, roc_auc_score print(os.listdir("D:RansomSecondApproachRansomware_Detection_using _CNNMixImages")) # Define Constants FAST_RUN = False IMAGE_WIDTH=256 # 150 accept maybe 256 IMAGE_HEIGHT=256 # maybe 256 IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT) IMAGE_CHANNELS=3 # maybe not need physical_devices = tf.config.experimental.list_physical_devices('GPU') print(physical_devices) if physical_devices: tf.config.experimental.set_memory_growth(physical_devices[0], True) # Prepare Traning Data filenames = os.listdir("D:RansomSecondApproachRansomware_Detection_using _CNNMixImages") categories = [] for filename in filenames: category = filename.split('l')[0] if category == 'image_benign_': categories.append(0) else: categories.append(1) df = pd.DataFrame({
  • 3. 'filename': filenames, 'category': categories }) print(df.head()) print(df.tail()) # in collab it will work df['category'].value_counts().plot.bar() model = Sequential() model.add(Conv2D(16, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS))) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(32, (3, 3), activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(BatchNormalization())
  • 4. model.add(Dropout(0.5)) model.add(Dense(2, activation='softmax')) # 2 because we have cat and dog classes model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Callbacks ## Early Stop To prevent over fitting we will stop the learning after 10 epochs and val_loss value not decreased earlystop = EarlyStopping(patience=10) # Learning Rate Reduction learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.5, min_lr=0.00001) callbacks = [earlystop, learning_rate_reduction] #Prepare data df["category"] = df["category"].replace({0: 'benign', 1: 'malware'}) train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42) train_df = train_df.reset_index(drop=True) validate_df = validate_df.reset_index(drop=True) total_train = train_df.shape[0] total_validate = validate_df.shape[0] batch_size= 15 # defualt 15 # Traning Generator # Defualt
  • 5. train_datagen = ImageDataGenerator( rotation_range=15, rescale=1./255, shear_range=0.1, zoom_range=0.2, horizontal_flip=True, width_shift_range=0.1, height_shift_range=0.1 ) ############################################### # Augmenting the training data train_generator = train_datagen.flow_from_dataframe( train_df, "D:RansomSecondApproachRansomware_Detection_using _CNNMixImages", x_col='filename', y_col='category', target_size=IMAGE_SIZE, class_mode='categorical', batch_size=batch_size ) # Validation Generator validation_datagen = ImageDataGenerator(rescale=1./255) validation_generator = validation_datagen.flow_from_dataframe(
  • 6. validate_df, "D:RansomSecondApproachRansomware_Detection_using _CNNMixImages", x_col='filename', y_col='category', target_size=IMAGE_SIZE, class_mode='categorical', batch_size=batch_size ) # See how our generator work example_df = train_df.sample(n=1).reset_index(drop=True) example_generator = train_datagen.flow_from_dataframe( example_df, "D:RansomSecondApproachRansomware_Detection_using _CNNMixImages", x_col='filename', y_col='category', target_size=IMAGE_SIZE, class_mode='categorical' ) # in collab it will work plt.figure(figsize=(12, 12)) for i in range(0, 15): plt.subplot(5, 3, i+1) for X_batch, Y_batch in example_generator:
  • 7. image = X_batch[0] plt.imshow(image) break plt.tight_layout() plt.show() # compile epochs = 1 # if FAST_RUN else 50 history = model.fit( train_generator, epochs=epochs, validation_data=validation_generator, validation_steps=total_validate//batch_size, steps_per_epoch=total_train//batch_size, # class_weight=class_weights, # add # shuffle=True, callbacks=callbacks ) model.save_weights("model.h5") # Virtualize Training ##in collab # fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12)) # ax1.plot(history.history['loss'], color='b', label="Training loss") # ax1.plot(history.history['val_loss'], color='r', label="validation loss")
  • 8. # ax1.set_xticks(np.arange(1, epochs, 1)) # ax1.set_yticks(np.arange(0, 1, 0.1)) # ax2.plot(history.history['acc'], color='b', label="Training accuracy") # ax2.plot(history.history['val_acc'], color='r',label="Validation accuracy") # ax2.set_xticks(np.arange(1, epochs, 1)) # legend = plt.legend(loc='best', shadow=True) # plt.tight_layout() # plt.show() # Prepare Testing Data test_filenames = os.listdir("D:RansomSecondApproachRansomware_Detection_using _CNNMixImages") test_df = pd.DataFrame({ 'filename': test_filenames }) nb_samples = test_df.shape[0] # Create Testing Generator # output Found 12500 images in kaggle. test_gen = ImageDataGenerator(rescale=1./255) test_generator = test_gen.flow_from_dataframe( test_df, "D:RansomSecondApproachRansomware_Detection_using _CNNMixImages", x_col='filename', y_col=None, class_mode=None,
  • 9. target_size=IMAGE_SIZE, batch_size=batch_size, shuffle=False ) # Predict predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size)) test_df['category'] = np.argmax(predict, axis=-1) label_map = dict((v,k) for k,v in train_generator.class_indices.items()) test_df['category'] = test_df['category'].replace(label_map) test_df['category'] = test_df['category'].replace({ 0: 'benign', 1: 'malware' }) # Virtaulize Result # Compute the average training and validation accuracy avg_train_acc = np.mean(history.history['accuracy']) avg_val_acc = np.mean(history.history['val_accuracy']) # Print the average training and validation accuracy print("Average training accuracy:", avg_train_acc) print("Average validation accuracy:", avg_val_acc) #in collab test_df['category'].value_counts().plot.bar() # See predicted result with images # in collab sample_test = test_df.head(18) sample_test.head()
  • 10. plt.figure(figsize=(12, 24)) for index, row in sample_test.iterrows(): filename = row['filename'] category = row['category'] img = load_img("D:RansomSecondApproachRansomware_Detection_using _CNNMixImages" +filename, target_size=IMAGE_SIZE) plt.subplot(6, 3, index+1) plt.imshow(img) plt.xlabel(filename + '(' + "{}".format(category) + ')' ) plt.tight_layout() plt.show() # Submission submission_df = test_df.copy() submission_df['id'] = submission_df['filename'].str.split('.').str[0] submission_df['label'] = submission_df['category'] submission_df.drop(['filename', 'category'], axis=1, inplace=True) submission_df.to_csv('submission.csv', index=False) First, please add the output of the F1 score, Precision, ROC AUC, and Cohen kappa to the code. Second, please add or modify the code for a higher result. I need coding only. Thank you in advance.