Skip to content

fixes failure when numpy version is 1.16.2. #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion classify_nsfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def main(argv):

if input_type == InputType.TENSOR:
if args.image_loader == IMAGE_LOADER_TENSORFLOW:
fn_load_image = create_tensorflow_image_loader(tf.Session(graph=tf.Graph()))
fn_load_image = create_tensorflow_image_loader(tf.Session(graph=tf.get_default_graph()))
else:
fn_load_image = create_yahoo_image_loader()
elif input_type == InputType.BASE64_JPEG:
Expand Down
2 changes: 1 addition & 1 deletion eval/batch_classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def main(argv):
input_type = InputType.TENSOR
model = OpenNsfwModel()

filenames = glob.glob(args.source + "/*.jpg")
filenames = sorted(glob.glob(args.source + "/*.jpg"))
num_files = len(filenames)

num_batches = int(num_files / batch_size)
Expand Down
6 changes: 5 additions & 1 deletion eval/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test(first, second):
result = {
'min': np.amin(delta),
'max': np.amax(delta),
'max-index': delta.argmax(),
'median': np.median(delta),
'mean': np.mean(delta),
'std': np.std(delta),
Expand Down Expand Up @@ -80,7 +81,10 @@ def main(argv):
other_classifications = classification_matrix(other)

print('SFW:')
print(test(original_classifications[:, 0], other_classifications[:, 0]))
result = test(original_classifications[:, 0], other_classifications[:, 0]);
print(result)
index = result["max-index"]
print(original[index])

print()
print('NSFW:')
Expand Down
85 changes: 85 additions & 0 deletions eval/eval_accurracy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import sys
import operator
import argparse
import numpy as np


def load_classifications(filename):
is_first = True

results = {}

with open(filename, 'r') as f:
for line in f:
if is_first:
is_first = False
continue

parts = line.split('\t')

filename = parts[0]
sfw_score = float(parts[1])
nsfw_score = float(parts[2])

results[filename] = (sfw_score, nsfw_score)

return results


def classification_matrix(classifications):
results = np.zeros(shape=(len(classifications), 2))

for i, classification in enumerate(classifications):
results[i] = np.array(classification[1])

return results


def test_acc(nsfw):
count = len(nsfw)
result = {
'sfw': (nsfw < 0.5).sum() / count,
'nsfw': (nsfw >= 0.7).sum() / count,
'sexy': ((nsfw >= 0.5) & (nsfw < 0.7)).sum() / count,
}

return result


def main(argv):
parser = argparse.ArgumentParser()

parser.add_argument("input",
help="File containing classifications")

parser.add_argument("-n", "--nsfw",
help="write out wrong NSFW classifications to file")

parser.add_argument("-s", "--sfw",
help="write out wrong SFW classifications to file")

args = parser.parse_args()
filename_input = args.input

result = load_classifications(filename_input)

result = sorted(result.items(), key=operator.itemgetter(0))

print("Found", len(result), "entries")

classifications = classification_matrix(result)

acc_res = test_acc(classifications[:, 1]);
print(acc_res)

if args.nsfw:
i = 0
with open(args.nsfw, 'w') as o:
for v in classifications[:, 1]:
if v < 0.7:
o.write('{}\n'.format(result[i][0]))
i += 1


if __name__ == "__main__":
main(sys.argv)
36 changes: 36 additions & 0 deletions eval/pick_wrong_test_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import sys
import argparse


def main(argv):
parser = argparse.ArgumentParser()

parser.add_argument("input",
help="File containing wrong images")

parser.add_argument("-s", "--source", required=True,
help="Folder containing the test images")

parser.add_argument("-o", "--output", required=True,
help="Folder to contain the wrong label images")

args = parser.parse_args()
filename_input = args.input

if os.path.exists(args.output) & (not os.path.isdir(args.output)):
print("{} exists but not a directory".format(args.output))
return
elif not os.path.exists(args.output):
os.mkdir(args.output)

with open(filename_input, 'r') as f:
for line in f:
line = line.rstrip()
source_file = os.path.join(args.source, line)
dest_file = os.path.join(args.output, line)
os.rename(source_file, dest_file)


if __name__ == "__main__":
main(sys.argv)
Loading