Advertisement
Guest User

simple GAN-generated anime pic detector (thiswaifudoesnotexist.com model v3)

a guest
Oct 24th, 2020
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import os
  2. import pandas as pd
  3. from PIL import Image
  4. import time
  5. import sys
  6.  
  7. def count_and_sort (df, columns):
  8. g = df.groupby (columns)
  9. return pd.DataFrame ({"count" : g.size ()}).reset_index ().sort_values ("count", ascending=False).reset_index ()
  10.  
  11. path = sys.argv[1]
  12. rows = []
  13. sizes = [(400, 400)]
  14. t = time.time ()
  15. columns = ["file", "score", "predicted_category"]
  16.  
  17. # test for discolorations in smooth areas/background
  18. def diff_score (values, limit):
  19. total = 0
  20. count = len (values) - 1
  21. for i in range (count):
  22. d = abs (values[i] - values[i + 1])
  23. if d < limit:
  24. total = total + d
  25. return total / count
  26.  
  27. pos = 0
  28. files = list (filter (lambda f: not f.startswith ("."), os.listdir (path)))
  29. for f in files:
  30. file = path + "/" + f
  31. score = 0
  32. for size in sizes:
  33. image = Image.open (file).resize (size)
  34. buf = [image.getdata (), image.rotate (90).getdata ()]
  35. image.close ()
  36. pixels = len (buf[0])
  37. for data in buf:
  38. r = [pixel[0] for pixel in data]
  39. g = [pixel[1] for pixel in data]
  40. b = [pixel[2] for pixel in data]
  41. r0 = diff_score (r, 2)
  42. g0 = diff_score (g, 2)
  43. b0 = diff_score (b, 2)
  44. score = score + r0 + g0 + b0
  45. rows.append ({"file" : f,
  46. "score" : score})
  47. pos = pos + 1
  48. if pos % 100 == 0:
  49. print (str (pos) + " images processed")
  50. print (str (time.time () - t) + " seconds elapsed")
  51. df = pd.DataFrame (rows)
  52. print (str (len (df.index)) + " images processed total")
  53. cutoff = 1.1266882918018237
  54. df["predicted_category"] = (df["score"] - cutoff).apply (lambda x: "GAN generated" if x > 0 else "real anime pic")
  55. df[columns].to_csv (sys.argv[2], index=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement