Skip to content

Instantly share code, notes, and snippets.

View xuwangyin's full-sized avatar
🎯
Focusing

xuwang xuwangyin

🎯
Focusing
View GitHub Profile
@xuwangyin
xuwangyin / eval_cifar10_autoattack.py
Created August 9, 2023 23:24
eval cifar10 wrn-28-10 robust model using autoattack, put the script into https://github.com/locuslab/robust_overfitting
import os
import argparse
from pathlib import Path
import warnings
import torch
import torch.nn as nn
import torchvision.datasets as datasets
import torch.utils.data as data
import torchvision.transforms as transforms
@xuwangyin
xuwangyin / gist:d472ac6a06ed3d7f0f542de294b4706d
Created July 28, 2023 01:04
A contextmanager that force the fixed width image output in jupyter notebook
from contextlib import contextmanager
import matplotlib.pyplot as plt
import base64
from io import BytesIO
from IPython.display import HTML, display
@contextmanager
def dp(width=600, dpi=300):
def display_figure():
# Save the current figure as a PNG image in memory
@xuwangyin
xuwangyin / eval_imagenet_resnet50_autoattack.py
Last active June 14, 2023 00:06
evaluate resnet50 models on imagenet using autoattack
import os
import argparse
from pathlib import Path
import warnings
import torch
import torchvision
import torch.nn as nn
import torchvision.datasets as datasets
import torch.utils.data as data
# coding: utf-8
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
import scipy
import scipy.stats
import numpy as np
from collections import Counter
import scipy
import numpy as np
from collections import Counter
# Kullback–Leibler divergence
# https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
# http://scipy.github.io/devdocs/generated/scipy.stats.entropy.html
def kl(p, q):
# compute common elements
@xuwangyin
xuwangyin / render_guardian_json_files.php
Created April 25, 2016 02:31
find my_guardian_json_file_dir -name "*.json" > guardian_json_files; php render_guardian_json_files.php guardian_json_files > guardian_articles.html; open guardian_articles.html;
<?php
error_reporting(E_ERROR | E_PARSE);
function get_content($body) {
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$content = "";
if ($doc->loadXML($body)) {
foreach ($doc->getElementsByTagName("body") as $t) {
foreach ($t->getElementsByTagName("paragraph") as $p) {
$content = $content . '<p>' . $p->textContent . '</p>';
@xuwangyin
xuwangyin / render_json_files.php
Created April 20, 2016 14:49
find my_json_file_dir -name "*.json" > json_files; php render_json_files.php json_files > articles.html; open articles.html;
<?php
error_reporting(E_ERROR | E_PARSE);
function get_content($body) {
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
if (!$doc->loadXML($body))
return $body;
$content = "";
foreach ($doc->getElementsByTagName("body") as $t) {
foreach ($t->getElementsByTagName("paragraph") as $p) {